Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicate from Watch to killed iOS app using WatchConnectivity

Using WatchOS2 and WatchConnectivity, is it possible to 'wake up' the iOS counterpart to send it a message using 'interactive messaging'?

The reason I ask is because I have a WatchOS1 app that is using openParentApplication to send/receive messages which actually wakes up the iOS app if it was not running, makes it receive my message and lets it send back a response. After that it closes down the iOS app again.

I am a bit confused as how to do the same kind of thing in WatchOS2. Because when I use 'interactive messaging' using WCSession the message I send is not delivered when I kill the iOS app.

I also noticed WatchOS2 now contains all sorts of frameworks you can use, like NSURLSession and EventKit which delegates all calls to the iPhone. So maybe that is the recommended way to go? My watch-app coincidentally only requires events from EventKit to operate. So I could rewrite the WatchConnectivity part of my app to use EventKit instead. And if my app would have to use HTTP, I could use NSURLSession directly.

Can anybody confirm that the intended way to communicate with the iPhone now in WatchOS2 is to use native frameworks like CoreData/EventKit/etc? It makes sense if it is, but I do like to have someone confirm this.

Thanks in advance.

like image 430
Tom van Zummeren Avatar asked Sep 21 '15 21:09

Tom van Zummeren


People also ask

What is WCSession?

The object that initiates communication between a WatchKit extension and its companion iOS app.

How do I use Apple Watch app on Xcode?

Open your iOS app's project in Xcode. Choose File > New > Target. Select the watchOS tab. Select “Watch App for iOS App” and click Next.


2 Answers

WCSession is the recommended way of communicating between the watch and iOS device.

For immediate communication use sendMessage:replyHandler:errorHandler: method of the WCSession instance. As per documentation:

Calling this method from your WatchKit extension while it is active and running wakes up the corresponding iOS app in the background and makes it reachable.

In cases when the other device can't be reached, e.g. because it's out of range, you may want to use updateApplicationContext:error: method instead. It will queue the data and deliver it later to the counterpart device.

like image 147
Krzysztof Szafranek Avatar answered Sep 17 '22 12:09

Krzysztof Szafranek


You have to make sure to set up the WCSession in a part of your iOS app that gets run when the iOS app is running in the background (UIApplicationDelegate application:didFinishLaunchingWithOptions: would be one of these spots, while any UIViewController's viewDidLoad would not be one of those)

like image 45
ccjensen Avatar answered Sep 18 '22 12:09

ccjensen