我在 AppleWatch 和 iPhone 之间进行通信时遇到问题。
iPhone to Watch 通讯工作正常。
观看 iPhone:
didReceiveApplicationContext
在 AppDelegate 中不会触发!!!
-
我在(iPhone)AppDelegate中有这个:
import WatchConnectivity
-
if WCSession.isSupported() {
WCSession.default.delegate = self
WCSession.default.activate()
}
...
extension AppDelegate: WCSessionDelegate {
// MARK: WCSessionDelegate
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
//
}
func sessionDidBecomeInactive(_ session: WCSession) {
//
}
func sessionDidDeactivate(_ session: WCSession) {
//
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String: Any]) {
DispatchQueue.main.async {
print("Phone didReceiveApplicationContext")
}
}
}
在手表 InterfaceController 我有:
import WatchConnectivity
-
if WCSession.isSupported() {
WCSession.default.delegate = self
WCSession.default.activate()
}
-
extension InterfaceController: WCSessionDelegate {
// MARK: WCSessionDelegate
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
//
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String: Any]) {
DispatchQueue.main.async {
print("Watch didReceiveApplicationContext")
}
}
}
-
我正在尝试通过“更新应用程序上下文”进行交流,如下所示:
@IBAction func buttonPressed() {
guard WCSession.isSupported() else {
return
}
let message = ["buttonPressed" : true]
do {
try WCSession.default.updateApplicationContext(message)
} catch {
print("Something went wrong")
}
}
-
为什么didReceiveApplicationContext
AppDelegate 中的方法不触发?
-
我发现调试 Watch 应用程序非常复杂,包括将调试器附加到 iPhone 应用程序等的必要性……也许我的调试方式存在根本性的问题?