有没有办法检测 Apple Watch 是否与 iPhone 配对?

Is there a way to detect if an Apple Watch is paired with an iPhone?

我想知道是否有可用的 API 显示 Apple Watch 的可用性。我还不想编写 Apple Watch App。我想做一些分析,看看有多少实际用户拥有 Apple Watch,然后再投入时间开发手表版本(当然,如果可能的话)。

答案是肯定的,但您必须支持 watchOS 2 并且 iOS9 才能做到。 您需要从 WCSession class.

检查 属性 paired

您可以在 Watch Connectivity Framework Reference. I also recommend to watch this video from WWDC 2015 and read this tutorial

找到所有信息

所以在 WatchOS 2 上这是可能的!

你必须在 iPhone 方面做 :

第一个:

import WatchConnectivity

然后:

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

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

希望对你有所帮助:)

Swift 5

import WatchConnectivity

然后:

final class WatchSessionManager: NSObject {

    private override init() {}

    static let shared = WatchSessionManager()

    func setup() {
        guard WCSession.isSupported() else {
            return
        }
        let session = WCSession.default
        session.delegate = self
        session.activate()
    }
}

extension WatchSessionManager: WCSessionDelegate {

    func sessionDidBecomeInactive(_ session: WCSession) {}

    func sessionDidDeactivate(_ session: WCSession) {}

    func session(
        _ session: WCSession, 
        activationDidCompleteWith activationState: WCSessionActivationState, 
        error: Error?
    ) {
        print("Apple Watch is paired: \(session.isPaired)")
    }

}