WatchOS2 连接框架不起作用

WatchOS2 connectivity framework doesn't work

我想将数据从 Iphone 传递到 Apple Watch。我尝试了所有方法,但是当我使用 didReceiveUserInfo 函数时,没有任何反应,我检查 WCSession 是否兼容。

我的 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")
                   }

我的 Apple Watch 上的代码:

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

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

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

}

WCSESSION 检查 Iphone:

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

            print("SUPPORT OK")
        }

WCSESSION 检查 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()

        }

我在 github 上创建了一个问题,并附上了建议的补丁。我在自己的设备上测试了这个版本的应用程序,手表收到了用户信息。我所做的主要更改是将 WCSessionDelegate 方法的声明从 "nested functions" 移动到文件中的顶级函数。嵌套函数只能在定义它们的函数范围内使用,这意味着委托对象不会实现这些方法。