ios - 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 check watchos 2 if application on iphone opened or not?

i want send message nsuserdefaults watch iphone via sendmessage (to able update interface on phone when message received) when both applications running , want send nsuserdefaults if watchos 2 app running.

from read found this:

/** counterpart app must reachable send message succeed. */ @property (nonatomic, readonly, getter=isreachable) bool reachable; 

it's reachable check.

reachable means apple watch , iphone connected via bluetooth or wifi. doesn't mean iphone app running. if reachable true, when try sendmessage apple watch launch iphone app in background. need assign wksession delegate possible because delegates methods (sendmessage) fire soon. think saying want call sendmessage if can, , not use transferuserinfo method instead. this, first on apple watch:

func applicationdidfinishlaunching() {     let session = wcsession.defaultsession()     session.delegate = self     session.activatesession()      // note: should custom message dictionary     // don't call following code in     // applicationdidfinishlaunching, here     // simplicity of example. call when want send message.     let message = [string:anyobject]()      // send message.     // check reachable here, change between reading     // value , sending message. instead try send data , if     // fails queue sent when connection re-established.     session.sendmessage(message, replyhandler: { (response) -> void in         // ios app got message     }, errorhandler: { (error) -> void in         // ios app failed message. send in background         session.transferuserinfo(message)     }) } 

then, in ios app:

// here setup possible // don't miss delegate method calls func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool {     self.watchkitsetup()       return true }  func watchkitsetup() {     // stop if watch connectivity not supported (such 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 message apple watch...     dispatch_async(dispatch_get_main_queue()) {         // update ui on main thread if necessary     } }  func session(session: wcsession, didreceiveuserinfo userinfo: [string : anyobject]) {     // handle message apple watch...     dispatch_async(dispatch_get_main_queue()) {         // update ui on main thread if necessary     } } 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -