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

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

问题描述

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.

documentation 也给出了一个正确的例子(从 Beta 3 开始).

The documentation gives a correct example as well (As of Beta 3).

别忘了同步数据库:

[yourDefaults synchronize];

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

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