Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'isReachable' is false when sending message from watch app to iOS app

I want to send instant message to iOS app from watch app. Implemented the following code in XCode7 beta 4 version and keeping the application in 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 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];
        });
    }

like image 452
Nagendra Avatar asked Jul 23 '15 09:07

Nagendra


3 Answers

Do you have to use the sendMessage APIs? I found them unreliable and unpredictable as well. I ended up using the applicationContext API's. The watch doesn't have to be reachable, but if it is, it arrives immediately, If not reachable, it gets delivered on app launch. Each time you update the application context, it overwrites the previous version, which might not be what you are looking for.

like image 70
Kyle Redfearn Avatar answered Nov 15 '22 22:11

Kyle Redfearn


I found in an iPhone app I am currently working on that I needed to have the WCSession activation code in both the 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, that does not marry with my understanding of what is supposed to be required, but it is what got session.reachable (Swift) to equal true

like image 28
Duncan Babbage Avatar answered Nov 15 '22 22:11

Duncan Babbage


First you should check if the Watch Connectivity Framework is linked correctly, also check your code. After that try with "Reset content and settings" from both simulators, this worked for me. In case it doesn't work yet, try uninstalling and reinstalling both apps from simulators. If it still doesn't work, try with removing the watch app extension from settings on the Watch app installed on the phone. Hope this helps!

like image 35
Omar Huanay Avatar answered Nov 15 '22 20:11

Omar Huanay