FrankvR
FrankvR

Reputation: 71

session(_:activationDidCompleteWith:error:) not called on Apple Watch (but is called in Simulator)

I've created a simple swift class:

import Foundation
import WatchConnectivity

class WatchCommunication : NSObject, WCSessionDelegate {

    var session: WCSession!

    override init() {
        super.init()

        if WCSession.isSupported() {
            print("WCSession is supported")
            self.session = WCSession.default
            self.session.delegate = self
            if session.activationState == WCSessionActivationState.activated {
                print("activationState is activated")
            } else {
                print("activationState is not activated")
                self.session.activate()
            }
        } else {
            print("WCSession is not supported")
        }
    }

    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        print("activationState", activationState.rawValue)
        print(error)
    }
}

In my Watch App Extension I load create an instance of this class

class ExtensionDelegate: NSObject, WKExtensionDelegate {

    let watchCommunication: WatchCommunication = WatchCommunication() 

    // ...

When I test this code in the simulator, I see the following logged

 WCSession is supported
 activationState is not activated
 activationState 2
 nil

All working fine. When I run the same application on my testing iPhone X and paired Apple Watch 3, the logs show

 WCSession is supported
 activationState is not activated

So it seems that the method

 func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {

is never called on my Apple Watch.

Methods like

 func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) { 

are also not called on device (but are working on the simulator).

On the iPhone X side of things, the actionDidCompleteWith method is called, with activationState 2 (activated) and updateApplicationContext does not throw an error.

There is however communication between the iPhone and Apple Watch, because the method https://developer.apple.com/documentation/healthkit/hkhealthstore/1648358-startwatchapp does indeed start a workout on the watch (and all Apple and third party watch apps work normally).

Upvotes: 0

Views: 750

Answers (1)

FrankvR
FrankvR

Reputation: 71

For further reference:

Restarting my Apple Watch (what I never do otherwise) did solve the issue...

Upvotes: 1

Related Questions