WatchConnectivity如何在多个WKInterfaceController之间共享会话? [英] WatchConnectivity how to share session among multiple WKInterfaceControllers?

查看:123
本文介绍了WatchConnectivity如何在多个WKInterfaceController之间共享会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我需要在多个WKInterfaceControllers之间共享WCSession.单例方法将不起作用,一旦将委托设置为一个类,其他类中的所有委托将失效.场景:接口A根据数据内容发送和接收数据,并显示接口B.点击接口B,将请求和接收其他数据.您将如何在A和B之间共享WCSession?

I have a situation where I need to share WCSession among multiple WKInterfaceControllers. Singleton approach won't work, once you set delegate to a class, all delegates in the other classes are invalidated. Scenario: interface A send and receive data, based on the data content, present interface B. Tap on interface B, will request and receive additional data. How would you share the WCSession between A and B ?

推荐答案

其他答案不能解释为应用范围的会话将起作用.

The other answer doesn't explain that an app-wide session would work.

可以使用整个应用范围内的WCSession单例,该单例可用于所有接口控制器.您只需在应用程序生命周期的早期就实例化会话管理器,并将其用作自己的委托即可.

You can use an app-wide WCSession singleton which would be available to all your interface controllers. You simply instantiate a session manager very early in the app life cycle, and let it be its own delegate.

会话管理器(单例)可以处理接口控制器的所有传输,而不必试图让每个接口控制器处理会话委托(这不能很好地工作).

Instead of trying to make each interface controller handle the session delegation (which won't work well), a session manager (singleton) can handle all the transfers for your interface controllers.

如另一个答案中所述,您可以使用通知让感兴趣的接口控制器知道其新数据何时到达.

As mentioned in the other answer, you could then use notifications to let interested interface controllers know when their new data arrives.

使用会话或数据管理器之类的模块化方法,有助于将此类代码保留在实际上不属于其的控制器之外.它还使测试和利用每个模块变得更加容易.

我不会在这里重复代码,因为Stack Overflow上已经发布了多个现有答案,以及 网络上的文章,其中详细介绍了此技术.例如:

I won't repeat the code here, as there have already been several existing answers posted on Stack Overflow, as well as articles on the web, which cover this technique in detail. For example:

WatchConnectivity:向WCSession打个招呼

您通常会在一些狭窄的问题中找到这些类型的答案,这些问题询问如何在手表应用程序及其并发症控制器之间共享数据.

这篇关于WatchConnectivity如何在多个WKInterfaceController之间共享会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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