问题
I'm trying to support the new feature for pairing with multiple watches in my iOS 9.3/watchOS 2.2 app. It seems to be working well, except that I can't figure out how the watchOS app can determine if the paired iPhone has been switched to another Apple Watch.
The docs say that the WCSession
stays activated from the perspective of the WatchKit extension throughout the switching lifecycle, and from my testing in Xcode, it seems that the watch session reports that the iPhone is also reachable even when the phone has been switched to another watch.
I don't currently have multiple watches to be able to check if the behavior is the same on an actual device.
回答1:
A specific answer to your question:
The fact that the watch is no longer connected is not readily apparent, as all transfer methods can be used, including immediate messaging. Transfers are simply deferred until the user switches back to that watch.
Since an unconnected watch can still initiate transfers to its paired iPhone, you would likely have to implement some sort of handshake or timeout detection, and see if the phone fails to respond in time.
My findings:
It seems that the unconnected watch is not meant to know that the user has switched to a different watch. As you pointed out, the watch app's session is still active, and the watch (simulator) still reports that phone is reachable.
An unconnected watch app can continue to use all transfer methods including interactive messaging (although outgoing data does get queued by the system, and is not transferred until the user switches back to that watch).
I've verified that data is queued, using two different approaches:
- by using
transferUserInfo
dictionary from a watch, letting the run loop execute, repeating that process a few times, then examining theoutstandingUserInfoTransfers
array. - by using a
sendMessage
reply handler which only runs after the user switches back to that watch, and a response is received.
Once switching back, all queued transfers are immediately sent to its phone, and any interactive responses are then received by the watch.
While an unconnected watch is able to queue data to its paired iPhone (since its session is still active), the phone can only communicate with its connected watch, since the phone no longer has an active session to any unconnected watch.
New sample code:
You can try out the new QuickSwitch sample code. It uses updateApplicationContext
to pass a designator and color from any of the watches to the phone.
You can change the designator and color for an unused watch, use the Xcode Devices pane to switch to that watch, then notice that the iOS app immediately shows that watch's new designator and color (which was queued while the watch was unused).
Possible reachable
documentation inconsistency:
In your WatchKit extension, the value of this property is YES when a matching session is active on the user’s iPhone and the device is within range so that communication may occur.
Since I don't have a second watch, I couldn't test whether an unused watch's reachability ever changes to false, once its phone moves out of range.
来源:https://stackoverflow.com/questions/36293124/how-can-watchos-2-2-app-determine-if-its-paired-iphone-has-switched-to-another-a