问题描述
iOS 8 昨天发布了一个关于 App Groups 的新 API.以前在应用程序之间共享数据和通信有点混乱,我相信这正是应用程序组旨在纠正的问题.
iOS 8 revealed a new API yesterday concerning App Groups. It was kind of messy before to share data and communicate between apps and I believe that's precisely what App Groups is intended to correct.
在我的应用程序中,我启用了应用程序组并添加了一个新组,但我找不到任何有关如何使用它的文档.文档和 API 参考仅说明了如何添加组.
In my app I have enabled App Groups and added a new group but I just can't find any documentation on how to use it. Documentation and API references only state how to add a group.
那么应用组的真正目的是什么?有没有关于如何使用它的文档?
So what is App Groups really intended to do? Is there any documentation somewhere on how to use it?
推荐答案
App Groups 的另一个好处是能够共享 NSUserDefaults
数据库.这也适用于应用扩展程序(通知中心小部件、自定义键盘等).
Another benefit to App Groups is the ability to share a NSUserDefaults
database. This also works for App Extensions (notification center widgets, custom keyboards, etc).
在应用组中的所有应用程序中像这样初始化您的 NSUserDefaults
对象,它们将共享数据库:
Initialize your NSUserDefaults
object like this in all applications in the app group and they will share the database:
目标-C:
[[NSUserDefaults alloc] initWithSuiteName:@"<group identifier>"];
斯威夫特:
NSUserDefaults(suiteName: "<group identifier>")
请记住,每个应用程序的 [NSUserDefaults standardUserDefaults]
数据库中的所有内容都不会转移到此数据库中.
Keep in mind everything from the [NSUserDefaults standardUserDefaults]
database for each application will not carry over into this database.
文档 也给出了一个正确的例子(从 Beta 3 开始).
The documentation gives a correct example as well (As of Beta 3).
并且不要忘记同步数据库:
And don't forget to synchronize the database:
[yourDefaults synchronize];
这篇关于使用应用程序组在应用程序之间通信和持久化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!