关闭/停用WCSession [英] Close/Deactivate WCSession

查看:125
本文介绍了关闭/停用WCSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有手表扩展功能(watchOS 2)的iPhone应用程序(iOS 9 beta),并将数据从手表传递到手机,我正在使用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个使用WCSession的不同视图控制器,因此对于每个控制器,我都实例化了新的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.

示例:

ViewController1:

ViewController1:

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

ViewController2:

ViewController2:

[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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆