Search code examples
swiftapple-watchwatchos-2wcsession

WatchOS2 connectivity framework doesn't work


I want to pass data from Iphone to Apple Watch. I tried everything but when I am using the didReceiveUserInfo function, nothing happens I check if WCSession is compatible and it is.

Code on my Iphone:

if(ipField.text != ""){
                do {
                    try watchSession?.transferUserInfo(["name" : "test"])
                    print("context update")

                } catch let error as NSError {
                    NSLog("Updating the context failed: " + error.localizedDescription)
                    print("failed")
                   }

Code on my Apple Watch:

func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]){

    let Value = userInfo["name"] as? String

    self.currentIpLabel.setText(Value)
    print("done1")

}

WCSESSION check Iphone:

  if (WCSession.isSupported()) {
            let session = WCSession.defaultSession()
            session.delegate = self
            session.activateSession()

            print("SUPPORT OK")
        }

WCSESSION check AppleWatch

  if(WCSession.isSupported()){
            watchSession = WCSession.defaultSession()
            // Add self as a delegate of the session so we can handle messages
            watchSession!.delegate = self
            watchSession!.activateSession()

        }

Solution

  • I have created an issue on github with a suggested patch attached. I tested this version of the app on my own devices and the watch received the userInfo just fine. The main change I made was to move the declaration of the WCSessionDelegate methods from being "nested functions" to top level functions in the file. Nested functions are only available from within the scope of the function they are defined in, which would mean that the delegate object wouldn't have implementations for those methods.