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

回眸只為那壹抹淺笑 提交于 2020-01-16 05:14:19

问题


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?)


回答1:


I've run into a similar issue (iOS 9.3, watchOS 2.2) where the session: didReceiveApplicationContext: delegate method wouldn't fire when expected. It appears there's some undocumented behavior whereby if the dictionary matches the previous value sent, the call to updateApplicationContext() fails silently, neither sending the dictionary nor throwing an error (see https://forums.developer.apple.com/thread/46107).

The solution offered in that thread was to add a NSUUID().UUIDString to every dictionary while testing. Worked for me.




回答2:


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.



来源:https://stackoverflow.com/questions/31474146/ios-watchos2-why-doesnt-sessiondidreceiveapplicationcontext-fire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!