Did you try sendMessage(_:replyHandler:errorHandler:)? Calling this method from your watchOS app while it is active and running wakes up the corresponding iOS app in the background and makes it reachable, which I believe is what you are looking for.
Consider a simple phone speedometer app. I use CoreLocation to get GPS data and show the speed on the phone screen. It doesn't need to do anything in the background.
Now I want to add watch support, sending the speed data from the phone to the watch using the applicationCOntext
. (Let's ignore the possibility that the watch app could do this using its own GPS.)
Now we need the phone app to run in the background, so that the watch can continue to show the speed when the phone is sleeping. So I enable the location background mode for the phone app and turn on CLLocationManager.allowsBackgroundLocationUpdates
.
This works but isn't ideal, because for users who don't have a watch etc. we are wasting power on the phone. So we really want to turn allowsBackgroundLocationUpdates
on only when the watch app is in use.
You suggest that I could use sendMessage(...)
to have the watch tell the phone that it is in use. So, on the phone I would implement didReceiveMessage
and change allowsBackgroundLocationUpdates
.
But... is it allowed for an app that has "when in use" location permission to turn on background location updates when it is already in the background? I guess it probably is, but I'll need to check.
So when exactly do I want the watch to send its "I'm in use, send data" and "I'm no longer in use, stop sending data" messages? I guess I send them from appropriate WKApplicationDelegate
methods. But I don't think I want the phone to turn its location updates on and off every time the user raises or lowers their arm. I guess I can send the messages that frequently, and have the phone app decide whether or not to turn updates off, perhaps with a timeout.
Ziqiao, any thoughts about this? Thanks.