WatchKit 2.0 从 Phone 发送消息以观看

WatchKit 2.0 Send Message From Phone To Watch

如果用户同时打开 Apple Watch 应用程序和 iPhone 应用程序,我希望能够更新我的 Apple Watch 视图。我知道有一个 ,但我想知道我是否可以使用 WatchConnectivity 来做到这一点。

在我的 iOS 应用程序中,我发送了一条消息:

if WCSession.isSupported() {
    // Set the session to default session singleton
    let session = WCSession.defaultSession()
    // Fire the message to watch
    NSLog("send message")

    session.sendMessage(["action": "messageAction"], replyHandler: nil, errorHandler: { (error) -> Void in
        // Display alert
        NSLog(error.description)
    })
}

但我一直收到错误消息:

Error Domain=WCErrorDomain Code=7007 "WatchConnectivity session on paired device is not reachable." UserInfo={NSLocalizedDescription=WatchConnectivity session on paired device is not reachable.}

从 iPhone 向 Apple Watch 发送消息,WatchConnectivity sendMessage 是正确的使用方法吗?

只有"correct"方法如果您希望与可访问的设备进行交互通信。

但是您没有显示您设置会话委托或激活会话的位置:

let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()

您可能还想添加一些检查以确保手表已配对 (session.paired)、手表应用程序已安装 (session.watchAppInstalled) 以及手表实际上可以访问 ( session.reachable) 在尝试与其交互通信之前,使您的应用程序更加健壮。

See this guide for more details.

您也可以选择回退到非交互式(后台)方法来为您的应用排队消息,直到它打开并可以接收它们。