iOS/watchos2 - 为什么不session:didReceiveApplicationContext:fire?

Dav*_*nte 1 session ios watchos-2

我已经阅读了下面的q/a,这很棒.这正是我在测试项目中所做的,它运行正常.

我现在已经创建了我的真实项目,但是在Watch扩展中,session: didReceiveApplicationContext:不会触发.

这是我的发送代码:

-(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");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是手表上的接收代码:

- (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);
    });

}
Run Code Online (Sandbox Code Playgroud)

使用watchOS2中的WatchConnectivity在iOS和WatchOS之间发送消息

我看过WWDC连接会话,发现这个网站非常有帮助.

任何想法(也许它不是代码,但缺少或不正确的plist设置?)

kha*_*len 6

我遇到了类似的问题(iOS 9.3,watchOS 2.2),其中session: didReceiveApplicationContext:委托方法在预期时不会触发.看起来有一些未记录的行为,如果字典与之前发送的值匹配,则调用updateApplicationContext()无声地失败,既不发送字典也不发送错误(请参阅https://forums.developer.apple.com/thread/46107).

该线程中提供的解决方案是NSUUID().UUIDString在测试时向每个字典添加一个.为我工作.