繁体   English   中英

检查 iPhone 是否与 Apple Watch 配对?

[英]Check if iPhone is paired with apple watch?

在我的应用程序中,

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

任何帮助表示赞赏。

因此,自从 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
        }
    }

我希望它会帮助你:)

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

这个想法来自@BilalReffas 的回答,但在 WatchOS 版本中大于 2.1 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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM