iosobjective-cwatchkitapple-watchwatchos-2

WatchOS 2.0: WCSessionDelegate not get called (in Simulator)


I am running WatchOS 2.0 on Version 7.0 beta 5. I am running an iOS with iWatch App.

I setup the Targets as shown.

enter image description here

I had my iOS's ViewController and WatchKitExtension's Interface Controller both activated WCSession and set as delegate.

if ([WCSession isSupported]) {
    WCSession *session = [WCSession defaultSession];
    session.delegate = self;
    [session activateSession];
    NSLog(@"iOS App WCSession is supported");
}

Then I tried to send userInfo from Watch to iOS :

NSDictionary *userInfo = [[NSDictionary alloc]initWithObjectsAndKeys:@"testingURL", @"outputURL", nil];
        [[WCSession defaultSession] transferUserInfo:userInfo];

But my ViewController's delegate method never get called:

- (void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary<NSString *,id> *)userInfo{

dispatch_async(dispatch_get_main_queue(), ^{
    NSLog(@"Received userInfo Transferr");
    NSLog(@"%@", userInfo);
    [self.label setText:@"Received"];
});
}

I was running the Watch App and iOS together from Simulator by pressing Run here from this scheme:

enter image description here

Could anyone please tell me what did I do wrong?


Solution

  • In general, it is not a good idea to receive WCSession data in a UIViewcontroller, since you never can be sure whether it is there or not.

    Apple says you should start receiving as soon as possible. Your UIApplicationDelegate is a good place to receive data from WCSession and a good place to set it up as early as possible.

    Edit

    You also don't hold a reference to your activated session on the watch side. This means Apple can remove all the session ressources.

    So you next call to defaultSession gets you a fresh unactivated session.

    Edit 2

    In my experience you have to do 2 things when testing the communication between WatchApp Extension and iOS App:

    1. Start WatchApp from XCode 7 (sometimes I have to do this twice)
    2. go into the iOS Simulator and start your iOS App manually

    There may be more ways to make sure both run and can communicate.

    Also try to send a message from iOS App to your WatchApp extension, this works for me.