【问题标题】:WatchKit 2.0 Send Message From Phone To WatchWatchKit 2.0 从手机发送消息观看
【发布时间】:2016-03-16 15:28:02
【问题描述】:

如果用户同时打开了 Apple Watch 应用程序和 iPhone 应用程序,我希望能够更新我的 Apple Watch 视图。我知道有一个WatchKit 1 question asked here,但我想知道我是否可以使用 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 的使用方法是否正确?

【问题讨论】:

    标签: ios swift watchkit watchos-2 watchconnectivity


    【解决方案1】:

    只有“正确”的方法如果您希望与可访问的设备进行交互通信。

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

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

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

    See this guide for more details.

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多