How to call a method in watchkit extension from ios parent application

We can call the openParentApplication:reply: method in the parent ios application from the clock set extension.

But is there a way to call the method in the watchkit extension from the parent ios application?

For example: in my application, when the user adds an event to the ios application, then the watchkit event list should also be updated, so for this I need to call the update method in the watchkit extension when the user adds a new event to the main application.

Please, help.

Thanks.

+5
source share
2 answers

You cannot directly call a method from the watchkit extension, but you can send a darwin notification (or use the MMWormhole library ( here ) and execute the correct method after receiving it.

+4
source

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 // Handle reply from watch (optional) }, errorHandler: nil) 

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"] { // Update UI } } 

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.

+1
source

Source: https://habr.com/ru/post/1210735/


All Articles