How can I check with watchOS 2 if the application on the iPhone is open or not and can send NSUserDefaults regardless of the status of the application?

How can I check from watchOS 2 if the application on the iPhone is open or not?

I want to send a message from NSUserDefaultswatch to iPhone via sendMessage(to be able to update the interface on the phone when I receive a message) when both applications are running, and I want to send NSUserDefaultseven if watchOS 2 is running.

From what I read, I found this:

/** The counterpart app must be reachable for a send message to succeed. */
@property (nonatomic, readonly, getter=isReachable) BOOL reachable;

It is always available from what I check.

+4
source share
2

, Apple iPhone Bluetooth Wi-Fi. , iPhone . , apple, iPhone . WKSession, (sendMessage). , , , sendMessage, , transferUserInfo. :

func applicationDidFinishLaunching() {
    let session = WCSession.defaultSession()
    session.delegate = self
    session.activateSession()

    // NOTE: This should be your custom message dictionary
    // You don't necessarily call the following code in
    // applicationDidFinishLaunching, but it is here for
    // the simplicity of the example. Call this when you want to send a message.
    let message = [String:AnyObject]()

    // To send your message.
    // You could check reachable here, but it could change between reading the
    // value and sending the message. Instead just try to send the data and if it
    // fails queue it to be sent when the connection is re-established.
    session.sendMessage(message, replyHandler: { (response) -> Void in
        // iOS app got the message successfully
    }, errorHandler: { (error) -> Void in
        // iOS app failed to get message. Send it in the background
        session.transferUserInfo(message)
    })
}

iOS:

// Do this here so it is setup as early as possible so
// we don't miss any delegate method calls
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.watchKitSetup()  
    return true
}

func watchKitSetup() {
    // Stop if watch connectivity is not supported (such as on iPad)
    if (WCSession.isSupported()) {
        let session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }
}

func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
    // Handle the message from the apple watch...
    dispatch_async(dispatch_get_main_queue()) {
        // Update UI on the main thread if necessary
    }
}

func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
    // Handle the message from the apple watch...
    dispatch_async(dispatch_get_main_queue()) {
        // Update UI on the main thread if necessary
    }
}
+6

, WatchConnectivity:

WCSession.updateApplicationContext()

, , . updateApplicationContext , .

WWDC 2015 WatchConnectivity: https://developer.apple.com/videos/wwdc/2015/?id=713

, , .

, , , , .

+3

All Articles