【问题标题】:Check if iPhone is paired with apple watch?检查 iPhone 是否与 Apple Watch 配对?
【发布时间】:2015-04-05 13:02:37
【问题描述】:

在我的应用中,

我需要查看我的手机是否与 Apple Watch 配对,并获取有关配对手表的一些信息,例如其名称。我尝试阅读文档,但似乎找不到任何特定于我的用例的内容。

感谢任何帮助。

【问题讨论】:

  • 很遗憾,目前无法检测 Apple Watch 是否已连接。
  • 我们可以在没有 Apple Watch 配套应用的情况下进行这种可达/连接检查吗?

标签: ios watchkit apple-watch


【解决方案1】:

所以自从 WatchOS 2 以来,这是可能的!

你必须在 iPhone 端做:

第一:

import WatchConnectivity

然后:

   if WCSession.isSupported() { // check if the device support to handle an Apple Watch
        let session = WCSession.default()
        session.delegate = self
        session.activate() // activate the session

        if session.isPaired { // Check if the iPhone is paired with the Apple Watch
                // Do stuff
        }
    }

希望对你有帮助:)

【讨论】:

  • 值得注意的是,在调用 paired(或 Obj-C 中的 isPaired)之前,您应该设置会话的委托并调用 activateSession()。如果您没有这样做,paired 将始终返回 false/NO。
  • 是否可以在没有手表应用的情况下检查可达性?
  • @SohilR.Memon 是的。我也回答了这个问题。
【解决方案2】:

您可以做的最好的事情是在用户第一次打开您的 WK 应用程序时写入一个共享的 NSUserDefaults 值,然后在您的 iOS 应用程序中检查该值。除此之外,您无法获得任何信息。

【讨论】:

  • 这似乎没问题!但是,如果想在我的 iphone 应用程序中根据 Apple Watch 是否连接而有另一种行为,那么上述解决方案可能没有帮助!
【解决方案3】:

这个想法来自@BilalReffas 的回答,但在高于 2.1 的 WatchOS 版本中,activate() 方法是异步的,因此提供的解决方案将不起作用(它总是返回 false,即使手表已连接)

首先导入SDK

import WatchConnectivity

然后执行会话激活请求

if WCSession.isSupported() { // check if the device support to handle an Apple Watch
    let session = WCSession.default
    session.delegate = self
    session.activate() // activate the session
}

然后实现来自WCSessionDelegate的方法

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    if activationState == .activated && session.isPaired { // Check if the iPhone is paired with the Apple Watch
        // Do stuff
    }
}

【讨论】:

    猜你喜欢
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 2018-12-21
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多