我想将数据从 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 上创建了一个问题,并附加了建议的补丁。我在自己的设备上测试了该版本的应用程序, watch 很好地收到了用户信息。我所做的主要更改是将 WCSessionDelegate 方法的声明从“嵌套函数”移至文件中的顶级函数。嵌套函数只能在定义它们的函数范围内使用,这意味着委托(delegate)对象不会实现这些方法。
关于swift - WatchOS2 连接框架不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33978892/