Swift Apple Watch WCConnectionLegate,在activationDidComplete中发送消息时偶尔会失败?
我遇到一个问题,通过WCConnection发送消息时,Swift Apple Watch WCConnectionLegate,在activationDidComplete中发送消息时偶尔会失败?,swift,watchkit,wcsession,Swift,Watchkit,Wcsession,我遇到一个问题,通过WCConnection发送消息时,会话.sendMessage如果在委托方法ActivationIDCompleteWith中调用,有时会失败。这个问题并非每次都可以重复(事实上,它在大多数情况下都有效) 但是,通过使用我的UI中的按钮(调用相同的加载代码)强制执行会话。sendMessage会立即成功进行会话通信,因此我知道问题不在会话本身或主应用程序中 假设会话已准备好接受ActivationIDCompleteWith中的通信是否不安全?有没有更好的地方可以给我的初次
会话.sendMessage
如果在委托方法ActivationIDCompleteWith
中调用,有时会失败。这个问题并非每次都可以重复(事实上,它在大多数情况下都有效)
但是,通过使用我的UI中的按钮(调用相同的加载代码)强制执行会话。sendMessage
会立即成功进行会话通信,因此我知道问题不在会话本身或主应用程序中
假设会话已准备好接受
ActivationIDCompleteWith
中的通信是否不安全?有没有更好的地方可以给我的初次交流打电话?根据我的经验,手表操作系统非常挑剔,尤其是在使用老式手表时。话虽如此,我认为问题的答案是:“假设会话准备好接受ActivationIDCompleteWith中的通信是否不安全?”是的,假设这样做是不安全的
在我自己的应用程序中,我有一个非常类似于你的情况,我通过发送消息直到收到响应来解决它
// false until a response is received from the phone
let receivedResponse: Bool = false
// function that sends the message
func requestResponse() {
guard WCSession.default.isReachable else {
print("Phone not reachable")
return
}
// callback that handles response
let responseHandler: ([String: Any]) -> () = { response in
receivedResponse = true
callback(response)
}
WCSession.default.sendMessage(["Request": "Response"],
replyHandler: responseHandler) { error in
print(error.localizedDescription)
}
}
// timer that calls the request function repeatedly
let retryTimer = Timer.scheduledTimer(withTimeInterval: 1,
repeats: true) { timer in
if receivedResponse {
// we know we got a response so clean up timer
timer.invalidate()
}
requestResponse()
}
事实上,我最终做了一些类似的事情,效果非常好,谢谢你的回答!