【发布时间】:2015-07-17 10:52:23
【问题描述】:
我已阅读下面的 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");
}
}
这是手表上的接收代码:
- (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
我观看了 WWDC 连接会话,发现 this site 非常有帮助。
任何想法(也许不是代码,而是缺少或不正确的 plist 设置?)
【问题讨论】: