【问题标题】:Is there a way to detect if an Apple Watch is paired with an iPhone?有没有办法检测 Apple Watch 是否与 iPhone 配对?
【发布时间】:2015-08-25 18:50:40
【问题描述】:

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

【问题讨论】:

    标签: ios iphone apple-watch


    【解决方案1】:

    所以在 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
            }
        }
    

    希望对你有帮助:)

    【讨论】:

    • 从 iOS 9.3 开始,您必须等待在委托上调用 session(_:activationDidCompleteWith:error:),然后才能从 session.paired 获得可靠值
    【解决方案2】:

    答案是肯定的,但你必须支持 watchOS 2 和 iOS9 才能做到这一点。 您需要检查属性pairedfrom WCSession 类。

    您可以在Watch Connectivity Framework Reference 找到所有信息。我还建议观看来自WWDC 2015 的视频并阅读此tutorial

    【讨论】:

      【解决方案3】:

      斯威夫特 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)")
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-07-27
        • 2016-01-30
        • 2018-12-21
        • 2010-11-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多