简体   繁体   English

iOS之间的应用程序组数据共享

[英]App Groups Data Sharing Between Applications iOS

Assuming I have the following app identifier com.test.product1 , which is of product1 offering a group id group.com.test.product1. 假设我有以下应用程序标识符com.test.product1 ,它是product1的,提供了组ID group.com.test.product1。

I am supposed to share a document to another product having com.test.product2 which is of product2 offering groupid group.com.test.product2. 我应该将文档共享到另一个具有com.test.product2的产品,该产品属于提供groupid group.com.test.product2的product2。

How can I share a common group say like group.com.test.product between all of these applications? 如何在所有这些应用程序之间共享一个常见的组,例如group.com.test.product

You can store shared data using App Groups, but I've only tried to do this with built in types. 您可以使用应用程序组存储共享数据,但是我仅尝试使用内置类型来实现。

If you're trying to store your own classes you may need to look at something like NSKeyedArchiver and NSCoding (maybe look here ). 如果您尝试存储自己的类,则可能需要查看诸如NSKeyedArchiver和NSCoding之类的内容(也许请看此处 )。

If you can find a way to use the built in types you can use App Groups as follows: 如果您找到使用内置类型的方法,则可以按以下方式使用应用程序组:

Sharing NSUserDefaults data between multiple apps 在多个应用程序之间共享NSUserDefaults数据

In order to have shared defaults between an app and an extension or between 2 apps you have to add an App Group in your settings using the following steps: 为了在一个应用程序和一个扩展程序或两个应用程序之间共享默认值,您必须使用以下步骤在设置中添加一个应用程序组:

  1. In the Project Navigator click on the *.xcodeproj file (should be at the top). 在项目浏览器中,单击* .xcodeproj文件(应位于顶部)。
  2. To the right of the Project Navigator look for Project and Targets. 在“项目浏览器”的右侧,找到“项目和目标”。 Under targets click on your primary target (should be the first thing under Targets). 在目标下,单击您的主要目标(应该是“目标”下的第一件事)。
  3. Towards the top, click on the Capabilities tab. 在顶部,单击“功能”选项卡。
  4. In the App Groups section click the switch to the right to turn App Groups ON. 在“应用程序组”部分中,单击右侧的开关以打开“应用程序组”。
  5. Click on the + button and add an App Group named group.com.company.myApp . 单击+按钮,然后添加一个名为group.com.company.myApp的应用程序组。
  6. Go to the same place in your other apps and this group should now be available to select. 转到其他应用程序中的同一位置,现在应该可以选择该组。 Turn this group on for each app that will be using this shared data. 为每个将使用此共享数据的应用程序打开此组。

Note: If you go to the Apple Developer Portal (the Apple website that shows all of your Certificates, Identifiers, Devices and Provisioning Profiles) and go to Identifiers > App Groups you should see this new App Group. 注意:如果您转到Apple开发人员门户(Apple网站显示所有证书,标识符,设备和供应配置文件)并转到标识符>应用程序组,则应该看到此新的应用程序组。

To store data: 要存储数据:

var userDefaults = NSUserDefaults(suiteName: "group.com.company.myApp")
userDefaults!.setObject("user12345", forKey: "userId")
userDefaults!.synchronize()

To retrieve data: 要检索数据:

var userDefaults = NSUserDefaults(suiteName: "group.com.company.myApp")
if let testUserId = userDefaults?.objectForKey("userId") as? String {
  print("User Id: \(testUserId)")
}

App groups is intended to share a bundle that can be used by the app and its extensions as a shared container, where you can store shared data: 应用程序组旨在共享一个捆绑包,该捆绑包可由应用程序及其扩展程序用作共享容器,您可以在其中存储共享数据:

Take a look at Core Data: Sharing data between Apps and their Widgets . 看一看Core Data:在Apps及其小部件之间共享数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM