Skip to content

Instantly share code, notes, and snippets.

@topherPedersen
Last active November 8, 2018 03:03
Show Gist options
  • Select an option

  • Save topherPedersen/5cafb8a7ec80e4146ce5a79fd8a53ee1 to your computer and use it in GitHub Desktop.

Select an option

Save topherPedersen/5cafb8a7ec80e4146ce5a79fd8a53ee1 to your computer and use it in GitHub Desktop.
Code Snippet from Page 32 of "Swift Development for the Apple Watch" (modified to accommodate breaking changes in swift/iOS since book was published)
//
// InterfaceController.swift
// Yo WatchKit Extension
//
// Created by Christopher Pedersen on 11/7/18.
// Copyright © 2018 Christopher Pedersen. All rights reserved.
//
import WatchKit
import Foundation
import WatchConnectivity
class InterfaceController: WKInterfaceController {
override func awake(withContext context: Any?) {
super.awake(withContext: context)
// Configure interface objects here.
let session = WCSession.default
session.delegate = self
session.activate()
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
// let session = WCSession.default
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
}
extension InterfaceController : WCSessionDelegate {
@available(watchOSApplicationExtension 2.2, *)
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
print("Session activated!")
}
func sessionDidDeactivate(session: WCSession) {
print("Session deactivated!")
}
func sessionDidBecomeInactive(session: WCSession) {
print("Session became inactive!")
}
func sessionReachabilityDidChange(_ session: WCSession) {
print("Reachability changed to \(session.isReachable)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment