Close/Deactivate WCSession

谁都会走 提交于 2019-12-23 17:12:01

问题


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

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