Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call the method in watchkit extension from parent ios app

We can call the method openParentApplication:reply: in parent ios app from watch kit extension.

But is there any way to call the method in watchkit extension from parent ios app ?

For example:In my app when user add event in ios app then watchkit event list also should refresh so for that i need to call the refresh method in watchkit extension when user add new event in main app.

Please help.

Thanks.

like image 752
pratik bhiyani Avatar asked Jan 09 '15 14:01

pratik bhiyani


2 Answers

You can't directly call a method from watchkit extension, but you can send a darwin notification (or use MMWormhole library (here), and execute proper method after recieving it.

like image 113
Axadiw Avatar answered Oct 22 '22 20:10

Axadiw


You can use the built-in WatchConnectivity framework to send message from iOS app to the paired Apple Watch.

1) First, activate watch connectivity session both in iOS app and WatchKit extension. On the iOS side it can be done in application didFinishLaunchingWithOptions of the app delegate. On watch side you can run this code in applicationDidFinishLaunching method of the WatchKit extension delegate.

if WCSession.isSupported() {
  let session = WCSession.defaultSession()
  session.delegate = self
  session.activateSession()
}

2) Now send a message from your iOS app.

let session = WCSession.defaultSession()

session.sendMessage(["message from iOS app":"🐥"], replyHandler: { reply in
  // Handle reply from watch (optional)      
}, errorHandler: nil)

3) Receive the message in your WatchKit extension by implementing the session didReceiveMessage method in your WCSessionDelegate delegate class.

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])
  }
}

Upon receiving the message from iOS we are sending a notification with postNotificationName method.

4) Subscribe to this notification in your InterfaceController that needs updating (or anywhere 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. This is where you can update your UI.

func didReceiveReloadNotification(notification: NSNotification) {
  let userInfo = notification.userInfo as? [String: String]
  if let userInfo = userInfo, data = userInfo["data"] {
    // Update UI
  }
}

Note: for the sake of readability I am using inline text string for the notification name "myapp.reload" and the message key "message from iOS app". But in the real app it is better to use properties for these text strings to avoid typos.

like image 42
Evgenii Avatar answered Oct 22 '22 20:10

Evgenii