关闭/停用 WCSession [英] Close/Deactivate WCSession
问题描述
我正在开发带有手表扩展程序 (watchOS 2) 的 iPhone 应用程序(iOS 9 测试版),并将数据从手表传递到手机,我正在使用 WCSession.
I am developing an iPhone app (iOS 9 beta) with watch extension (watchOS 2), and to pass the data from watch to phone I am using WCSession.
我有 2 个使用 WCSessions 的不同视图控制器,因此对于每个控制器,我正在实例化新的 WCSession 对象.对于第一个视图控制器它工作正常,但是当我想在第二个视图控制器中接收消息时,仍然很少有初始消息被发送到第一个控制器.
I have 2 different view controllers using WCSessions, so for each of the controller I am instantiating new WCSession object. For the first view controller it works fine, but when I want to receive messages in second view controller, few initial messages are still being sent to first controller.
在转到第二个控制器之前,有什么方法可以停用/禁用第一个视图控制器的会话?或者我还有其他选择吗?
Is there any way I can deactivate / disable session of first view controller before going to second controller? Or are there any other options I should look into?
谢谢!
推荐答案
当您来回传递数据时,您就是在发送字典.如果您指定好的键,您可以获得每个 ViewController 的适当数据.
When you are passing data back and forth you are sending Dictionaries. If you specify good keys you could get the appropriate data for each ViewController.
示例:
视图控制器1:
[session updateApplicationContext:@{@"viewController1": @"item1"} error:&error];
视图控制器2:
[session updateApplicationContext:@{@"viewController2": @"item2"} error:&error];
当您接收数据时:
- (void) session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {
if ([applicationContext objectForKey:@"viewController1"]) {
//ViewController1 data
} else if ([applicationContext objectForKey:@"viewController2"]) {
//ViewController2 data
}
}
查看这里的答案以了解有关 WC 的更多信息 在 watchOS2 中使用 WatchConnectivity 在 iOS 和 WatchOS 之间发送消息
Look at the answer here to learn more about WC Send messages between iOS and WatchOS with WatchConnectivity in watchOS2
这篇关于关闭/停用 WCSession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!