检查 iPhone 是否与 Apple Watch 配对?

Check if iPhone is paired with apple watch?

在我的应用中,

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

感谢任何帮助。

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

所以自从 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
        }
    }

希望对你有所帮助:)

这个想法取自@BilalReffas 的回答,但是在 WatchOS 2.1 以上的版本中 activate() 方法是异步的,所以提供的解决方案将不起作用(它总是 returns 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
    }
}