问题
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.
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?
Thanks!
回答1:
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.
Example:
ViewController1:
[session updateApplicationContext:@{@"viewController1": @"item1"} error:&error];
ViewController2:
[session updateApplicationContext:@{@"viewController2": @"item2"} error:&error];
When you are receiving data:
- (void) session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {
if ([applicationContext objectForKey:@"viewController1"]) {
//ViewController1 data
} else if ([applicationContext objectForKey:@"viewController2"]) {
//ViewController2 data
}
}
Look at the answer here to learn more about WC Send messages between iOS and WatchOS with WatchConnectivity in watchOS2
来源:https://stackoverflow.com/questions/31496584/close-deactivate-wcsession