我正试图在我的iPhone (运行iOS 9.1)和Apple (Version2.0.1)之间建立与Swift中的WatchConnectivity API的连接。
我遵循本教程,无法在设备之间实现消息传递。
来自Apple Watch的信息:
let applicationData = ["data":sampleData]
self.wcSession.sendMessage(applicationData, replyHandler: {(_: [String : AnyObject]) -> Void in
// handle reply from iPhone app here
}, errorHandler: {(error ) -> Void in
// catch any errors here
})在我的ViewController.swift:
// MARK: - WatchConnectivity Session
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
let sample:HKQuantitySample = (message["data"] as? HKQuantitySample)!
print("Sample messaged: \(sample)")
}
func sessionReachabilityDidChange(session: WCSession) {
print("session reachability changed: \(session.reachable)")
}手表应用程序和iOS应用程序都是前台!!我不知道缺了什么。
发布于 2015-12-03 17:00:58
所有将字典作为参数的WCSession API都只接受属性列表类型的字典;这包括您正在使用的sendMessage API:
要发送的属性列表值的消息/字典。定义对应方支持的字典的内容。此参数不得为零。
因此,HKSamples不是一个属性列表类型,这就是为什么它不能工作的原因,尽管您说错误处理程序没有被调用,这听起来很可疑。您确定将代码更改为此不会记录任何内容吗?
self.wcSession.sendMessage(applicationData, replyHandler: {(_: [String : AnyObject]) -> Void in
// handle reply from iPhone app here
}, errorHandler: {(error ) -> Void in
print(error);
})https://stackoverflow.com/questions/34051505
复制相似问题