1
在啓動時,我讓手錶發送消息給iOS設備,讓應用知道它準備好接收更新的數據。有時候這段代碼可以正常工作,有時候不會。watchOS,WCSession activationDidCompleteWith「activated」,但無法到達
經常失敗,出現以下調試輸出:
16:12:15.150|WatchSessionManager|WC Session activated with state: 2 (0 - notActivated, 1 - inactive, 2 - activated)
16:12:15.194|WatchSessionManager|Not reachable
基於我在蘋果文檔讀過一旦連接後,您可以發送這樣的現場消息。爲什麼在連接被激活時失敗?
謝謝。
僅供參考,下面的代碼被稱爲ExtensionDelegate:
func applicationDidFinishLaunching() {
WatchSessionManager.sharedManager.startSession() //connect to iPhone
}
代碼:
func startSession() {
if WCSession.isSupported() {
let session = WCSession.default()
session.delegate = self
session.activate()
}
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if let error = error {
print("WC Session activation failed with error: \(error.localizedDescription)")
return
}
debug.log(tag: "WatchSessionManager", content: "WC Session activated with state: \(activationState.rawValue) (0 - notActivated, 1 - inactive, 2 - activated)")
if WCSession.default().isReachable {
debug.log(tag: "WatchSessionManager", content: "Requesting")
let message = ["request": "masterList"]
WCSession.default().sendMessage(message, replyHandler: { (replyDict) -> Void in
//do stuff
}, errorHandler: { (error) -> Void in
self.debug.log(tag: "WatchSessionManager", content: "Send to iOS app failed with error \(error)")
})
}
else {
debug.log(tag: "WatchSessionManager", content: "Not reachable")
}
}
你檢查這些條款嗎? https://developer.apple.com/reference/watchconnectivity/wcsession/1615683-isreachable – abjurato