David DelMonte David DelMonte - 5 months ago 75
iOS Question

iOS/watchos2 - Why doesn't session:didReceiveApplicationContext: fire?

I have read the q/a below, and it's great. This is exactly what I'm doing in a test project, and it works fine.

I've now created my real project, but in the Watch extension,

session: didReceiveApplicationContext:
does not fire.

Here is my send code:

-(void)sendPlistToWatch:(NSMutableDictionary *)dictionary {
NSLog(@"%s", __FUNCTION__);
if ([WCSession defaultSession]) {
NSDictionary *applicationDict = @{@"Favorites.plist":dictionary};
[[WCSession defaultSession] updateApplicationContext:applicationDict error:nil];

NSLog(@"sent dictionary");
} else {
NSLog(@"not paired");
}
}


And this is the receive code on the watch:

- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];

if ([WCSession isSupported]) {
[self.session activateSession];
self.session = [WCSession defaultSession];
self.session.delegate = self;
}
}

- (void)willActivate {
[super willActivate];
}

- (void)didDeactivate {
[super didDeactivate];
}



- (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {
NSString *string = [applicationContext objectForKey:@"dictionary"];

NSMutableDictionary *dictionary = [applicationContext objectForKey:@"dictionary"];

dispatch_async(dispatch_get_main_queue(), ^{
NSLog (@"applicationContext: %@", applicationContext);
});

}


Send messages between iOS and WatchOS with WatchConnectivity in watchOS2

I've watched the WWDC connectivity session, and find this site very helpful.

Any ideas (perhaps it's not code, but a missing or incorrect plist setting?)

Answer

Found the cause...

I did activate the session, but it was too late in the process to be called.

I changed the code to report an error:

-(void)sendPlistToWatch:(NSMutableDictionary *)dictionary {
    NSLog(@"%s", __FUNCTION__);
    NSError *error = nil;
    if ([WCSession defaultSession]) {
        NSDictionary *applicationDict = @{@"StationFavorites.plist":dictionary};
        [[WCSession defaultSession] updateApplicationContext:applicationDict error:&error];
        if (error) {
             NSLog(@"Problem: @%@", error);
        } else {
             NSLog(@"sent dictionary");
        }

    } else {
        NSLog(@"not paired");
    }
}

That reported error 7004: WCErrorDomain Code=7004The operation couldn’t be completed. (WCErrorDomain error 7004.)"`

I reset the session to appear atop ViewDidLoad and all is well.

Comments