On start up I make the watch send a message to the iOS device to let the app know its ready to receive updated data. Sometimes this code works fine, sometimes it doesn't.
It often fails with the following debug output:
16:12:15.150|WatchSessionManager|WC Session activated with state: 2 (0 - notActivated, 1 - inactive, 2 - activated)
16:12:15.194|WatchSessionManager|Not reachable
Based on what I've read in the Apple docs once the connection is activated, you can send live messages like this. Why is it failing when the connection has been activated?
Thanks.
For reference, the below code is being called in ExtensionDelegate:
func applicationDidFinishLaunching() {
WatchSessionManager.sharedManager.startSession() //connect to iPhone
}
The code:
func startSession() {
if WCSession.isSupported() {
let session = WCSession.default()
session.delegate = self
session.activate()
}
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if let error = error {
print("WC Session activation failed with error: \(error.localizedDescription)")
return
}
debug.log(tag: "WatchSessionManager", content: "WC Session activated with state: \(activationState.rawValue) (0 - notActivated, 1 - inactive, 2 - activated)")
if WCSession.default().isReachable {
debug.log(tag: "WatchSessionManager", content: "Requesting")
let message = ["request": "masterList"]
WCSession.default().sendMessage(message, replyHandler: { (replyDict) -> Void in
//do stuff
}, errorHandler: { (error) -> Void in
self.debug.log(tag: "WatchSessionManager", content: "Send to iOS app failed with error \(error)")
})
}
else {
debug.log(tag: "WatchSessionManager", content: "Not reachable")
}
}
For those in a similar boat, I never ended up finding the cause of this problem. However, as a bit of a workaround, I looped up my start session function, to attempt a new connection every couple of seconds when a connection has failed, and it needs to send/receive data. The second connection attempt was basically successful every time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With