How to check whether iPhone and apple watch are co

2019-01-14 11:42发布

Is there any way to notify user in Apple Watch that the iPhone is now out of range and when it comes back in range. How can we do it in watch extension.

Thanks in advance.

5条回答
小情绪 Triste *
2楼-- · 2019-01-14 12:06

From a formal perspective, Apple have not given any indication of how this will be handled.

However, given the pairing and communication area handled by the OS without app involvement, it seems almost certain that any notifications to the user regarding connection issues on the watch (and at the phone end) will be handled by the Watch OS as well. My guess would be that a user will be given an opportunity to resolve the loss of connectivity, or to quit the Watch app if they cannot. From a developer perspective, it is highly likely our apps will not be able to distinguish between an unresolved loss of connectivity and the user quitting an app normally, with the same notification being sent to the Watch Extension for either, but this is only a guess.

It should be noted that there is no third party developer code running on the watch for the current Watch apps, just a UI, so even an unresolved loss of connection will not result in any data loss. If the Watch Extension (which runs on the iPhone) is quit by the OS due to loss of connection to the watch, it will still be able to do its usual data storage and cleanup.

查看更多
乱世女痞
3楼-- · 2019-01-14 12:11

You can find all connectivity state change notification in the WCSession documentation.

查看更多
Fickle 薄情
4楼-- · 2019-01-14 12:17

With watchOS 2.0 you can. To do this you would add these to your ExtensionDelegate if you wanted your Apple Watch to get a notification:

func watchKitSetup() {    
    if (WCSession.isSupported()) {
        let session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()

        // In your WatchKit extension, the value of this property is true when the paired iPhone is reachable via Bluetooth.
        // On iOS, the value is true when the paired Apple Watch is reachable via Bluetooth and the associated Watch app is running in the foreground.
        // In all other cases, the value is false.
        if session.reachable {

        }
    }
}

func applicationDidFinishLaunching () {
    self.watchKitSetup()
}

// Called when session.reachable value changes, such as when a user wearing an Apple Watch gets out of range of their iPhone.
func sessionReachabilityDidChange(session: WCSession) {
    if session.reachable {

    }
}

You should also add WCSessionDelegate to your ExtensionDelegate.

查看更多
【Aperson】
5楼-- · 2019-01-14 12:25

So on WatchOS 2 that is possible !

You have to do on iPhone side :

First :

import WatchConnectivity

Then :

   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
        }
    }

I hope It would help you :)

查看更多
爷、活的狠高调
6楼-- · 2019-01-14 12:26

From the current point of knowledge this will probably not be possible.

From Apple's WatchKit App Architecture

After choosing the scene, WatchKit tells the paired iPhone to launch your WatchKit extension and create the objects needed to manage that scene. When the scene is fully configured, it is displayed on Apple Watch. The transfer of information between the WatchKit app and WatchKit extension happens transparently behind the scenes.

That means, the code is executed on the iPhone. If the iPhone is out of reach it will not be possible to run the app on the watch.

查看更多
登录 后发表回答