Now in touch with a project developed by Apple iOS, use the keychain to implement data sharing between apps published under the same signature. Now share the experience.
The project was carried out in the XCODE6.2 development environment.
There are two ways to access keychain in iOS, and neither of these methods can be mixed, or there will be problems that cannot be shared. However, the obtained method can be generalized, the following first gives the function prototype of keychain access.
KeyChain.h
+ (void) Save: (NSString *) service data: (ID) data;+ (ID) Load: (NSString *) service;+ (void) Delete: (NSString *) service;
Keychain.m
Private method, search keychain DataSet.
+ (NSMutableDictionary *) Getkeychainquery: (NSString *) Service {return [nsmutabledictionary Dictionarywithobjectsandkeys: (__BRIDG e-ID) Ksecclassgenericpassword, (__bridge ID) ksecclass, service, (__bridge ID) ksecattrservice, service , (__bridge ID) ksecattraccount, (__bridge ID) ksecattraccessibleafterfirstunlock, (__bridge ID) ksecattraccessible , nil];}
//Save Keychain data , data in the <span style= "font-family:arial, Helvetica, Sans-serif;" An object of type >nsmutabledictionary, the key in the object is the child key that you need to share the data. </span>+ (void) Save: (NSString *) service data: (ID) data {//get Search dictionary nsmutabledictionary *keychainq Uery = [self getkeychainquery:service]; Delete old item before add New Item Secitemdelete ((__bridge cfdictionaryref) keychainquery); Add new object to search dictionary (attention:the data format) [Keychainquery setobject:[nskeyedarchiver Archiveddata Withrootobject:data] Forkey: (__bridge ID) ksecvaluedata]; ADD item to keychain with the search Dictionary Secitemadd ((__bridge cfdictionaryref) keychainquery, NULL);}
Loads the keychain data, returns an object of type Nsmutabledictionary, passes the child key through the Objectforkey method in nsmutabledictionary to obtain the corresponding shared data. + (ID) Load: (NSString *) Service {ID ret = nil; Nsmutabledictionary *keychainquery = [self getkeychainquery:service]; Configure the search setting//since in our simple case we is expecting only a single attribute to be returned (the Password) We can set the attribute ksecreturndata to Kcfbooleantrue [keychainquery setobject: (ID) kcfbooleantrue Forkey: (__bridge ID) ksecreturndata]; [Keychainquery setobject: (__bridge ID) ksecmatchlimitone forkey: (__bridge ID) ksecmatchlimit]; Cfdataref keyData = NULL; if (secitemcopymatching (__bridge cfdictionaryref) keychainquery, (Cftyperef *) &keydata) = = NOERR) {@try { ret = [Nskeyedunarchiver unarchiveobjectwithdata: (__bridge NSData *) KeyData]; } @catch (NSException *e) {NSLog (@ "Unarchive of%@ failed:%@", service, E); } @finally {}} if (KeyData) cfrelease (kEydata); return ret;}
Delete the keychain data, the service here is the primary key, so the deletion is all the data. + (void) Delete: (NSString *) Service { Nsmutabledictionary *keychainquery = [self getkeychainquery:service]; Secitemdelete ((__bridge cfdictionaryref) keychainquery);}
Below, if you configure Keychain.
The first method: through the plist way
1. Create a new file and select the property list file.
2, create a new sub-node, named Keychain-access-groups
3, modify the node keychain-access-groups type is array
4. Add sub-nodes under the Keychain-access-groups node and fill in your primary key name
Second approach: Configure directly via the Xcode feature (very simple)
1. Select the Capabilities page in the project configuration
2. Open the keychain sharing switch and set the name of your primary key.
Note: The above two types cannot be mixed, and the first method will be used to deal with keychain sharing.
Keychain Access in iOS development.