Close / Disable WCSession

I am developing an iPhone app (iOS 9 beta) with a watch extension (watchOS 2) and I am using WCSession to transfer data from phone to chat.

I have two different view controllers using WCSessions, so for each of the controllers I create a new WCSession object. For the first view controller, it works fine, but when I want to receive messages in the second view controller, some initial messages are still sent to the first controller.

Is it possible to disable or disconnect the session of the first controller view before moving to the second controller? Or are there any other options I should look into?

Thank!

+3


source to share


1 answer


When you pass data back and forth, you are sending dictionaries. If you provide good keys, you can get the appropriate data for each ViewController.

Example:

ViewController1:

[session updateApplicationContext:@{@"viewController1": @"item1"} error:&error];

      

ViewController2:



[session updateApplicationContext:@{@"viewController2": @"item2"} error:&error];

      

When you receive 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
    }
}

      

Check out the answer here to learn more about WC Send messages between iOS and WatchOS with WatchConnectivity in watchOS2

+1


source







All Articles