You can use the built-in WatchConnectivity infrastructure to send messages from an iOS application to a paired Apple Watch.
1) First activate the session connection to the session in both the iOS application and the WatchKit extension. On the iOS side, this can be done in the application didFinishLaunchingWithOptions delegate. On the view side, you can run this code in the applicationDidFinishLaunching WatchKit extension delegate method.
if WCSession.isSupported() { let session = WCSession.defaultSession() session.delegate = self session.activateSession() }
2) Now send a message from the iOS application.
let session = WCSession.defaultSession() session.sendMessage(["message from iOS app":"🐥"], replyHandler: { reply in
3) Receive a message in your WatchKit extension by embedding the session didReceiveMessage method in your WCSessionDelegate delegate WCSessionDelegate .
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) { if let message = message["message from iOS app"] { NSNotificationCenter.defaultCenter().postNotificationName("myapp.reload", object: self, userInfo: ["data": message]) } }
After receiving a message from iOS, we send a notification using the postNotificationName method.
4) Subscribe to this notification in your InterfaceController that needs updating (or somewhere else where you want to receive this update notification).
override func awakeWithContext(context: AnyObject?) { super.awakeWithContext(context) NSNotificationCenter.defaultCenter().addObserver(self, selector: "didReceiveReloadNotification:", name: "myapp.reload", object: nil) } deinit { NSNotificationCenter.defaultCenter().removeObserver(self, name: "myapp.reload", object: nil) }
5) Finally, implement the notification handler method. Here you can update your interface.
func didReceiveReloadNotification(notification: NSNotification) { let userInfo = notification.userInfo as? [String: String] if let userInfo = userInfo, data = userInfo["data"] {
Note: for readability, I use the built-in text string for the notification name "myapp.reload" and the message "message from the iOS application." But in a real application, it's best to use properties for these text strings to avoid typos.