Reputation: 2519
I'm developing a WatchApp that needs to communicate with the iOS App and I'm using the Watch Connectivity fwk to do that.
Each time the user displays my WatchApp I need to get from the iOS App a new set of data that must be displayed on the Watch.
To get these data I'm using sendMessage(_:replyHandler:errorHandler:) but sometimes it seems this communication is not working because my WatchApp is not updated. I have no issue when the WatchApp has been started from Xcode, issues appear only when I'm using it in the real life.
I trigger the synchronization between the WatchApp and the iOS App each time my WatchApp is callback on:
In the video "Introducing Watch Connectivity" for the 2015 WWDC, the speaker tells the WatchKit Extension must be running foreground when sending an interactive message to the iOS App.
Does it mean I need to check the WKExtention.shared().applicationState == .active before to call sendMessage(_:replyHandler:errorHandler:), in addition of WCSession.activationState == .activated ?
When my WatchApp is in the Dock and the user displays the Dock, my WatchApp has a WKExtension.shared().applicationState == .inactive and WKExtension.shared().isApplicationRunningInDock == true, Can I use sendMessage(_:replyHandler:errorHandler:) to update my App in this state?
I'm just wondering which conditions I need to check before to use sendMessage(_:replyHandler:errorHandler:).
Thanks,
Upvotes: 2
Views: 323
Reputation: 610
When i send watch connectivity messages I always check session.activationState == .activated && session.isReachable. Also I usually send an initial message and wait for the phone to respond before requesting data. This seems to make responses more reliable than requesting data initially for some reason. Here is an example
func sendActivationMessage() {
if session.activationState == .activated && session.isReachable {
session.sendMessage(["Watch Message" : "Activate"], replyHandler: { (reply) in
// Send message requesting data
}, errorHandler: { (error) in
print("***** Error Did Occur: \(error) *****")
})
} else {
print("***** Activation Error *****")
}
}
If print("***** Activation Error *****") gets called then you know that the phone is not reachable.
Upvotes: 0