Sharing data between iOS apps

Source: Internet
Author: User

We know that iOS because of the sandbox, the application can not go across its own area to access the content of other storage space, but there may be many scenarios we need to share data between the application, such as multiple applications shared user name password to sign in and so on. Although we can not directly through the file system to share data, but there are some methods can be achieved, in order to facilitate the explanation, here at the same time created two project send and receive, to achieve the information sharing between the two apps, send responsible for writing data, receive responsible for reading data, The specific demo code can be obtained here

Uipasteboard

The Clipboard is an easy way to pass data between applications, and it is recommended that you do not use the global pasteboard, but instead create a new pasteboard based on your name to prevent the effects of global copies elsewhere. Then copy the content that needs to be shared to the Clipboard, the content of the pasteboard can be text, URL, picture and Uicolor etc., another app can read the relevant information according to the name of the pasteboard.

Send to set the contents of the Pasteboard:
Uipasteboard *pasteboard = [Uipasteboard pasteboardwithname:@ "Mypasteboard" create:yes];
pasteboard.string = @ "Mysharedata";

Receive reads the contents of the Pasteboard:

Uipasteboard *pasteboard = [Uipasteboard pasteboardwithname:@ "Mypasteboard" create:no];

NSString *content = pasteboard.string;

Custom URL Scheme

Share data with OpenURL see http://www.cnblogs.com/qiaomc/p/5818445.html link

Shared Keychain Access
iOS keychain provides a way to store information securely, save data such as passwords, and the data in keychain is not lost as you remove the app, and you can continue to read the data in keychain after reinstalling. Typically, each application only allows access to the data it has stored in keychain, but if you use the same certificate, different apps can also be keychain to share data between applications.
In order to implement keychain shared data, we need to turn on keychain sharing, open the method as follows, and then add the same keychain Group, but don't forget to add security.framework.

Note: Both send and receive two items require the keychain sharing to be turned on, The xxxxx.entitlements file will be generated after opening, and the bundle ID of the send must be added to the Xxxxx.entitlements file in the receive project to share the data properly.

Send

Receive

Send save data to keychain (for simple use of Sskeychian)

[Sskeychain setpassword:@ "Sharedata" forservice:@ "MyService" account:@ "QMC"];

Receive read data

NSString *mydata = [Sskeychain passwordforservice:@ "MyService" account:@ "QMC"];

APP Groups

IOS8 after Apple joined the app groups feature, the application can share resources through the same group, the app group can share the small amount of data through Nsuserdefaults, If you need to share a larger file can be nsfilecoordinator, nsfilepresenter, and so on.
To open the app groups, you need to add a group Name,app to share the data with this group:

The Send project sets the content according to the group name:

Nsuserdefaults *mydefaults = [[Nsuserdefaults alloc]initwithsuitename:@ "Group.cn.goldenshiel.shareData"];
[Mydefaults setobject:@ "shared data" forkey:@ "MyKey"];

Receive reads data based on group name

Nsuserdefaults *mydefaults = [[Nsuserdefaults alloc]initwithsuitename:@ "Group.cn.goldenshiel.shareData"];
NSString *content = [mydefaults objectforkey:@ "MyKey"];

Sharing data between iOS apps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.