ios - WatchConnectivity - 使用 sendMessage
<p><p>我正在尝试使用 Swift 中的 WatchConnectivity API 在 Apple Watch(2.0.1 版)和我的 iPhone(运行 iOS 9.1)之间建立连接。</p>
<p>我关注了<a href="http://www.kristinathai.com/watchos-2-tutorial-using-sendmessage-for-instantaneous-data-transfer-watch-connectivity-1/" rel="noreferrer noopener nofollow">this tutorial</a>并且无法实现设备之间的消息传递。</p>
<p>来自 Apple Watch 的消息:</p>
<pre><code> let applicationData = ["data":sampleData]
self.wcSession.sendMessage(applicationData, replyHandler: {(_: ) -> Void in
// handle reply from iPhone app here
}, errorHandler: {(error ) -> Void in
// catch any errors here
})
</code></pre>
<p>在我的 ViewController.swift 中:</p>
<pre><code>// MARK: - WatchConnectivity Session
func session(session: WCSession, didReceiveMessage message: , replyHandler: () -> Void) {
let sample:HKQuantitySample = (message["data"] as? HKQuantitySample)!
print("Sample messaged: \(sample)")
}
func sessionReachabilityDidChange(session: WCSession) {
print("session reachability changed: \(session.reachable)")
}
</code></pre>
<p>Watch app 和 iOS app 都是前台!!
我不确定缺少什么。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>所有将字典作为参数的 WCSession API 只接受 <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html" rel="noreferrer noopener nofollow">property list types</a> 的字典。 ;这包括 <a href="https://developer.apple.com/library/prerelease/ios/documentation/WatchConnectivity/Reference/WCSession_class/index.html#//apple_ref/occ/instm/WCSession/sendMessage:replyHandler:errorHandler:" rel="noreferrer noopener nofollow">sendMessage API</a>您正在使用:</p>
<blockquote>
<p><strong>message</strong> / A dictionary of property list values that you want to send. You define the contents of the dictionary that your counterpart supports. This parameter must not be nil.</p>
</blockquote>
<p>所以 HKSamples 不是 <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html" rel="noreferrer noopener nofollow">property list type</a>这就是为什么这不起作用的原因,尽管您说错误处理程序没有被调用,这听起来很可疑。您确定将代码更改为此不会记录任何内容吗?</p>
<pre><code>self.wcSession.sendMessage(applicationData, replyHandler: {(_: ) -> Void in
// handle reply from iPhone app here
}, errorHandler: {(error ) -> Void in
print(error);
})
</code></pre></p>
<p style="font-size: 20px;">关于ios - WatchConnectivity - 使用 sendMessage,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34051505/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34051505/
</a>
</p>
页:
[1]