使用应用程序组在应用程序之间传递和保存数据 [英] Communicating and persisting data between apps with App Groups

查看:157
本文介绍了使用应用程序组在应用程序之间传递和保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

那么App Groups真正打算做什么?是否有任何关于如何使用它的文档?

So what is App Groups really intended to do? Is there any documentation somewhere on how to use it?

推荐答案

App Groups的另一个好处是能够共享 NSUserDefaults 数据库。这也适用于App Extensions(通知中心小部件,自定义键盘等)。

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:

Objective-C:

Objective-C:

[[NSUserDefaults alloc] initWithSuiteName:@"<group identifier>"];

Swift:

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];

这篇关于使用应用程序组在应用程序之间传递和保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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