Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i check from watchOS 2 if application on iPhone is opened or not and be able to send NSUserDefaults no matter the app status?

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

I want to send a message with NSUserDefaults from watch to iPhone via sendMessage (to be able to update interface on phone when message received) when both applications are running and I want to send NSUserDefaults even if only watchOS 2 app 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's always reachable from what I check.

like image 809
Silviu St Avatar asked Aug 19 '15 12:08

Silviu St


1 Answers

Reachable means the apple watch and iPhone are connected via bluetooth or wifi. It doesn't necessarily mean the iPhone app is running. If reachable is true, when you try to sendMessage from the apple watch it will launch the iPhone app in the background. You need to assign the WKSession delegate as soon as possible because the delegates methods (sendMessage) will fire soon. I think what you are saying you want to do is call sendMessage if you can, and it not use the transferUserInfo method instead. To do this, first on your apple watch:

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

Then, in your iOS app:

// 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
    }
}
like image 59
lehn0058 Avatar answered Oct 05 '22 20:10

lehn0058