WCSession sendMessage not working

919 views Asked by At

I'm calling this code in my Watch extension (and it does run, checked with a breakpoint):

 [[WCSession defaultSession] sendMessage:@{
              @"load": @"main"
              } replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {
                  ...
                  }
              } errorHandler:^(NSError * _Nonnull error) {
                  ...
              }];

In my parent app, I've got:

if([WCSession isSupported]){
    NSLog(@"Apple Watch detected, activated watch deletage.");
    [WCSession defaultSession].delegate = self;
    [[WCSession defaultSession] activateSession];
}

The above code runs when the app is launched, and I've implemented:

-(void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message replyHandler:(void (^)(NSDictionary<NSString *,id> * _Nonnull))replyHandler{
    ...
}

But it is never called. I've once got it to work, but now, I can't debug my Watch app and iOS app together properly (they don't launch, timeout etc), I deploy them one after other. When I'm debugging Watch extension, I can see the sendMessage... method is called (and after a few minutes, my error handler is called with a timeout error), so no issue there. When I launch my main app, I also see that WCSession related code is being hit. However, when I launch my Watch app to communicate with it, didReceivedMessage... is never called.

I've deleted my app from both iPhone and Watch, restarted both devices, and restarted my Mac and tried again, but no avail. What am I doing wrong?

1

There are 1 answers

0
Can Poyrazoğlu On

Even though Watch is not really reliable, it was a simple memory allocation issue in my case. I wasn't holding a strong reference to my WCSession's delegate, and it was getting deallocated before the method was called. I've created a strong reference and the problem went away.