WatchKit App
- import WatchConnectivity and conform to WCSessionDelegate.
- use the static session delegate via
WCSession.default()
.- Send data to the iPhone app using:
WCSession.default().sendMessage(message, replyHandler:_ errorHandler:_)
- The message object should be a dictionary of type [String:Any]
- If you are looking for data to be returned from the Watch app, provide the logic in a closure defined in the replyHandler; otherwise, pass in nil.
- To respond to messages sent from the iPhone app, you will use the WCSessionDelegate callback method
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Swift.Void){...}
- These methods will not be required in your Watch app to properly conform to the WCSessionDelegate:
func sessionDidBecomeInactive(_ session: WCSession) func sessionDidDeactivate(_ session: WCSession) func sessionWatchStateDidChange(_ session: WCSession)
And finally, it is generally best practice to store any common images, the images and assets that will continually be used for the Watch app to be placed in the Watch's xcassets folder. Okay, now that you are confused, let's get to the details!
It might be a surprise to you, but you need to import WatchConnectivity again.
import WatchConnectivity
Next, you need to verify that the session is even possible.
// MARK: - View Life Cycle Callbacks override func awake(withContext context: Any?) { super.awake(withContext: context) // Configure interface objects here. if WCSession.isSupported() { WCSession.default().delegate = self WCSession.default().activate() print("InterfaceController: Session Activated") // Request Data from iPhone App let requestMessage = ["message":"get-data"] WCSession.default().sendMessage(requestMessage, replyHandler: { (replyMessage) in print("Got a reply from the phone: \(replyMessage)") // handle reply message here }, errorHandler: { (error) in print("Got an error sending to the phone: \(error)") }) } else { print("\nViewController: connectionManager is nil\n") } }
But none of this will work, unless you implement methods required for the WCSessionDelegate.
extension InterfaceController : WCSessionDelegate { func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { print("1. InterfaceController: ", "activationDidCompleteWith activationState") // first } /** ------------------------- Interactive Messaging ------------------------- */ func sessionReachabilityDidChange(_ session: WCSession) { print("2. InterfaceController: ", "sessionReachabilityDidChange") // second } func session(_ session: WCSession, didReceiveMessage message: [String : Any]) { print("3. InterfaceController: ", "didReceiveMessage") } func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Swift.Void) { // third print("4. InterfaceController: ", "didReceiveMessage") //print("Message Contents: ", message["message"]!) } func session(_ session: WCSession, didReceiveMessageData messageData: Data) { print("5. InterfaceController: ", "didReceiveMessageData") } func session(_ session: WCSession, didReceiveMessageData messageData: Data, replyHandler: @escaping (Data) -> Swift.Void) { print("6. InterfaceController: ", "didReceiveMessageData") } /** -------------------------- Background Transfers ------------------------- */ func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) { print("7. InterfaceController: ", "didReceiveApplicationContext") } func session(_ session: WCSession, didFinish userInfoTransfer: WCSessionUserInfoTransfer, error: Error?) { print("8. InterfaceController: ", "didFinish userInfoTransfer") } func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) { print("9. InterfaceController: ", "didReceiveUserInfo") } func session(_ session: WCSession, didFinish fileTransfer: WCSessionFileTransfer, error: Error?) { print("10. InterfaceController: ", "didFinish fileTransfer") } func session(_ session: WCSession, didReceive file: WCSessionFile) { print("11. InterfaceController: ", "didReceive file") } }