"isReachable" is incorrect when sending a message from a watch application to an iOS application

I want to send an instant message to an iOS app from a watch app. The following code has been introduced in the version of XCode7 beta 4 and saving the application in the foreground in both simulators. here is the code i implemented

In watchkit interfaceController

     - (void) willActivate 
     {
         [super willActivate];
         if ([WCSession isSupported]) {
             WCSession * session = [WCSession defaultSession];
             session.delegate = self;
             [session activateSession];
         }
     }

 - (IBAction) buttonClicked
 {
     NSDictionary * applicationDict = [[NSDictionary alloc] initWithObjects: @ [@ "Hi"] forKeys: @ [@ "key"]];
     if ([[WCSession defaultSession] isReachable])
     {
         [[WCSession defaultSession] sendMessage: applicationDict
                                    replyHandler: ^ (NSDictionary * reply) {

                                        NSLog (@ "% @", reply);

                                    }

                                    errorHandler: ^ (NSError * error) {

                                        NSLog (@ "% @", error);

                                    }];
     }
 }


In the iOS app class

     - (void) viewDidLoad 
     {
         [super viewDidLoad];
         if ([WCSession isSupported]) {
             WCSession * session = [WCSession defaultSession];
             session.delegate = self;
             [session activateSession];
         }
     }


     - (void) session: (nonnull WCSession *) session 
     didReceiveMessage: (nonnull NSDictionary *) message replyHandler: (nonnull void (^) (NSDictionary * __nonnull)) replyHandler 
     {
         dispatch_async (dispatch_get_main_queue (), ^ {
             self.testLbl.text = [message objectForKey: @ "key"];
             [self.view setNeedsDisplay];
         });
     }

+6
source share
3 answers

Do you need to use the sendMessage API? I found them unreliable and unpredictable. I ended up using applicationContext API. The clock does not have to be available, but if it is, it arrives immediately, if it is not available, it is delivered when the application starts. Each time you update the application context, it overwrites the previous version, which may not be what you are looking for.

+4
source

I found in the iPhone application in which I am currently working that I need a WCSession activation code in both AppDelegate and the current View Controller ....

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([WCSession isSupported]) { WCSession *session = [WCSession defaultSession]; session.delegate = self; [session activateSession]; } ... 

Like you, this does not mean marrying my understanding of what should be required, but it is what session.reachable (Swift) got true

+2
source

First you need to check if the Watch Connectivity Framework is bound correctly, and also check your code. After that, try with “Reset content and settings” from both simulators, it worked for me. If it still does not work, try uninstalling and reinstalling both applications from the simulators. If it still does not work, try removing the watch app extension from the settings in the Watch app installed on your phone. Hope this helps!

+1
source

All Articles