【问题标题】:Check if Apple watch is currently paired with iPhone检查 Apple Watch 当前是否与 iPhone 配对
【发布时间】:2022-07-27 05:47:29
【问题描述】:

如果我当前的 Apple Watch 与 iPhone 配对,我正在尝试签入一个“独立”的 WatchKit 应用。

我尝试通过使用 WatchConnectivity 框架来做到这一点,但不幸的是,该框架不支持 watchOS 上的 isPaired 方法。另外,isReachable 方法不适用于此应用,因为 iPhone 上没有对应的应用。

非常感谢任何解决方案或解决方法。

【问题讨论】:

    标签: ios swift watchkit watchos


    【解决方案1】:

    首先导入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
        }
    }
    

    【讨论】:

    • 感谢丹尼尔的回答。正如我所提到的,isPaired 方法在 watchOS 中不可用。
    【解决方案2】:

    你能在 ios15 中使用相同的代码吗?

    【讨论】:

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