首先,我使用“transferUserInfo”方法将字典从 iPhone 发送到 Apple Watch:
let dicty = //...my dictionary of property-list values...
if WCSession.isSupported() {
let session = WCSession.defaultSession()
if session.paired == true { // Check if your Watch is paired with your iPhone
if session.watchAppInstalled == true { // Check if your Watch-App is installed on your Watch
session.transferUserInfo(dicty)
}
}
}
然后我使用以下委托回调方法“didFinishUserInfoTransfer”来检查传输状态:
func session(session: WCSession, didFinishUserInfoTransfer userInfoTransfer: WCSessionUserInfoTransfer, error: NSError?) {
if error == nil {
let session = WCSession.defaultSession()
let transfers = session.outstandingUserInfoTransfers
if transfers.count > 0 { //--> is always > 0, why ?????????
for trans in transfers {
trans.cancel() // cancel transfer that will be sent by updateApplicationContext
let dict = trans.userInfo
session.transferUserInfo(dict) //--> creates enless-transfer cycle !!!!!
}
}
}
else {
print(error)
}
}
在 Apple 文档中,它是关于 didFinishUserInfoTransfer 方法的:
The session object calls this method when a data transfer initiated by the
current app finished, either successfully or unsuccessfully. Use this method
to note that the transfer completed or to respond to errors, perhaps by
trying to send the data again at a later time.
到目前为止一切顺利 - 我明白了。但现在 - 有一些我不明白:
如果输入了 didFinishUserInfoTransfer 并且错误 == nil,那么究竟为什么 session.outstandingUserInfoTransfers COUNT 可以大于零??????
根据 Apple 文档,didFinishUserInfoTransfer 的唯一非错误状态应该是传输结束时!有点它似乎没有结束......为什么?
感谢您对此的任何澄清。
而且,我很高兴有任何关于如何正确使用这 3 种方法的示例代码!(IE
session.transferUserInfo(dicty)
didFinishUserInfoTransfer
session.outstandingUserInfoTransfers)