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't share data directly from the file system, there are some ways to do it.
Here, we create a new two projects, T1: Responsible for writing data, T2: responsible for reading data.
Method One: 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.
T1 to set the contents of the Clipboard:
Uipasteboard *pasteboard = [Uipasteboard pasteboardwithname:@ "mypasteboard" Create: YES];p Asteboard. string @" Mysharedata ";
T2 Read Clipboard contents:
Uipasteboard *pasteboard = [Uipasteboard pasteboardwithname:@ "mypasteboard" *content = pasteboard. string;
Method Two: Share the Keychain
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 saves in keychain, but if you use the same certificate, different apps can share data between apps through Keychain.
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: The timing of keychain cleanup: 1, reset the phone, 2, manual cleanup using code. In addition to these two cases, other conditions will not be cleaned up.
T1 app to save data to Keychain,t2 app reads data from keychain:
#import "ViewController.h"#import<SAMKeychain/SAMKeychain.h>@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //T1 App save data to Keychain[Samkeychain SetPassword:@"qwe"Forservice:@"SSS"Account@"QQQ"];}@end
#import "ViewController.h"#import<SAMKeychain/SAMKeychain.h>@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //T2 application reads data from keychainNSString *s = [Samkeychain passwordforservice:@"SSS"Account@"QQQ"]; NSLog (@"%@", s);}@end
Method Three: 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.
Open app groups, two apps T1 and T2 all need to add an identical group Name,app to share data across this group.
#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //T1 Create signal settings and set up data with AppGroup's nameNsuserdefaults *mydefaults = [[Nsuserdefaults alloc]initwithsuitename:@"Group.yss"]; [Mydefaults setobject:@"Shared Data"Forkey:@"MyKey"];}@end
#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //T2 Create bias settings and read data based on AppGroup's nameNsuserdefaults *mydefaults = [[Nsuserdefaults alloc]initwithsuitename:@"Group.yss"]; NSString*content = [Mydefaults objectforkey:@"MyKey"]; NSLog (@"%@", content);}@end
Method Four: Pass the custom URL Scheme
This is mainly applied to the knowledge of the jump, it is not here to do too much explanation.
Share data between different apps