0

I am trying to implement messaging between Watch and iPhone where the watch is the one sending data to phone. It has to happen in background therefore I'm using applicationContxt. However, I keep getting these errors whenever I try to run the code: Sometimes it works once and the phone receives data then the errors start.

[WC] -[WCSession handleApplicationContextWithPairingID:]_block_invoke delegate (null) does not implement session:didReceiveApplicationContext:

[WC] WCSession is missing its delegate
import WatchConnectivity

class PhoneWorkingSet: NSObject,  WCSessionDelegate {

    override init() {
        super.init()
       }
    
    func startSession() {
       if WCSession.isSupported() {
        let session = WCSession.default
        session.delegate = self
        session.activate()
        }
    }
    

    func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
    
        print("Received: \(applicationContext)")
    }
    
    
    
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        if(!session.isReachable) {
            print("Watch not reachable")
        } else {
            print("Watch Reachable")
        }
    }
    
    func sessionDidBecomeInactive(_ session: WCSession) {
        print("has contentPending: \(session.hasContentPending)")
    }
    
    func sessionDidDeactivate(_ session: WCSession) {
        
    }
  

}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • Where are you calling `startSession` from? Are you even calling it? – Dávid Pásztor Mar 03 '21 at 11:20
  • Yes i'm calling it from the SceneDelegate in func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) – Jessy1999 Mar 03 '21 at 11:26
  • 1
    SOLVED! It was wrong to start session in func scene() because it very likely kept getting re-instantiated which caused the error. I changed the code so when init() is called the session is started and instantiated the session in SceneDelegate not inside a function. – Jessy1999 Mar 03 '21 at 13:42
  • Glad you found a solution. If you update your question with the original (non-working code) you had for calling `startSession` and then write an answer yourself, containing the working code, this can be a useful self-answered Q&A for future readers. – Dávid Pásztor Mar 03 '21 at 14:04

0 Answers0