在扩展之间共享UserDefaults [英] Sharing UserDefaults between extensions

查看:305
本文介绍了在扩展之间共享UserDefaults的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建了Today小部件,我正在使用UserDefaults(suiteName:)保留一些数据.在主应用程序中,我正在使用UserDefaults.standard().这是扩展名无法读取(或可以吗?)的原因,这就是为什么我使用suiteName:构造函数.

Creating a Today widget and I am using UserDefaults(suiteName:) to persist some data. In the main application I am using UserDefaults.standard(). This can't be read (or can it?) by the extension which is why I use the suiteName: constructor.

用户在主应用中坚持到UserDefaults.standard()的数据需要在扩展程序中可用.

Data that user persist to UserDefaults.standard() in the main app needs to be available in the extension.

这一次我坚持两者,以便可以共享值

At this time I am persisting to both so that the values can be shared

 UserDefaults.standard().set:...forKey:...
 UserDefaults(suiteName:...)().set:...forKey:...
 ...

问题是我应该一起丢弃UserDefaults.standard()并在应用程序中使用UserDefaults(suiteName:),还是这种不好的做法?为什么?

Question is should I drop UserDefaults.standard() all together and just use UserDefaults(suiteName:) in my application, or is this bad practice and if so why?

我正在使用应用程序组容器.为了澄清起见,我问我是否在整个项目中都用suiteName:替换standard()?

I am using an App group container. For clarification I am asking should I just replace standard() with suiteName: throughout my project?

推荐答案

您不能使用UserDefaults.standard在主机应用程序及其应用程序扩展之间共享数据.相反,您必须使用UserDefaults(suiteName:)创建一个共享容器来共享数据.

You cannot use UserDefaults.standard to share data between a host app and its app extension. You instead have to create a shared container with UserDefaults(suiteName:) to share data.

即使一个应用程序扩展程序包嵌套在其包含的内容中 应用程序包,正在运行的应用程序扩展程序和包含应用程序的应用程序都没有 直接访问对方的容器.

Even though an app extension bundle is nested within its containing app’s bundle, the running app extension and containing app have no direct access to each other’s containers.

要启用数据共享,请使用Xcode或Developer Portal启用 包含应用及其包含的应用扩展程序的应用组. 接下来,在门户中注册应用程序组并指定该应用程序组 在包含的应用中使用.

To enable data sharing, use Xcode or the Developer portal to enable app groups for the containing app and its contained app extensions. Next, register the app group in the portal and specify the app group to use in the containing app.

启用应用程序组后,一个应用程序扩展及其包含的应用程序 都可以使用NSUserDefaults API共享对用户的访问 优先.要启用此共享,请使用initWithSuiteName:方法 实例化一个新的NSUserDefaults对象,传入标识符 共享组中的一个.

After you enable app groups, an app extension and its containing app can both use the NSUserDefaults API to share access to user preferences. To enable this sharing, use the initWithSuiteName: method to instantiate a new NSUserDefaults object, passing in the identifier of the shared group.

有关更多信息,请参阅:

For more, refer to: https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1

如何使用应用程序组: https://github.com/pgpt10/Today-Widget

How to use App Groups: https://github.com/pgpt10/Today-Widget

标准或套房名称?

对仅用于主机应用程序的数据使用标准之一.将suiteName用于要在Extension和Host App之间共享的数据.只是不要在两个数据中都保留相同的数据.避免数据冗余.根据上下文使用它们.

Use standard one for data that is only for Host App. Use suiteName for data that you want to share between Extension and Host App. Just don't persist the same data in both of them. Avoid data redundancy. Use both of them according to the context.

这篇关于在扩展之间共享UserDefaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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