This article provides a full version of the demo, Source: GitHub Demo Download
Keychain is a safe way for Apple to save user names, passwords, and certificates, and after storing sensitive information in keychain, this information will not be lost as the app is uninstalled, unless the developer manually deletes the sensitive information in the app, This information will be kept in keychain.
When using keychain, we first introduce security.framework into the project. Because arc is not supported in use, we need to enable MRC mode for the relevant files in the ARC environment.
First, we construct a tool class that uses this class to manipulate keychain.
#import <Foundation/Foundation.h> #import <Security/Security.h> @interface keychain:nsobject//Save Username and password to keychain+ (void) Save: (NSString *) service data: (ID) data;//take out username and Passwore from key chain+ (ID) Load: (NSString *) service;//Delete username and password from keychain+ (void) Delete: (NSString *) service; @end
In the implementation file, we write:
#import "KeyChain.h" @implementation KeyChain/** * This class needs to work in MRC mode, ACR projects follow these steps * Select Project->TARGETS-&G The corresponding target then selects "Build phases" on the right and the "Compile Sources" is found down. Then add after the corresponding file:-fno-objc-arc parameter * **/+ (nsmutabledictionary *) Getkeychainquery: (NSString *) Service {return [Nsmutabledictionary Dictionarywithobjectsandkeys: (ID) Ksecclassgenericpassword, (id) ksecclass, serv ICE, (ID) ksecattrservice, service, (ID) ksecattraccount, (ID) ksecattraccessibleafterfirstunlock, (ID) k Secattraccessible, nil];} #pragma mark write + (void) Save: (NSString *) service data: (ID) data {//get Search dictionary nsmutabledictionary *keychai Nquery = [self getkeychainquery:service]; Delete old item before add New Item Secitemdelete ((CFDICTIONARYREF) keychainquery); Add new object to search dictionary (attention:the data format) [Keychainquery setobject:[nskeyedarchiver Archiveddata Withrootobject:data] Forkey: (ID) ksecvalUedata]; ADD item to keychain with the search Dictionary Secitemadd ((cfdictionaryref) keychainquery, NULL);} #pragma mark Read + (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: (ID) Ksecreturndata]; [Keychainquery setobject: (ID) ksecmatchlimitone Forkey: (ID) ksecmatchlimit]; Cfdataref keyData = NULL; if (secitemcopymatching (Cfdictionaryref) keychainquery, (Cftyperef *) &keydata) = = NOERR) {@try {re t = [Nskeyedunarchiver unarchiveobjectwithdata: (NSData *) KeyData]; } @catch (NSException *e) {NSLog (@ "Unarchive of%@ failed:%@", service, E); } @finally {}} if (KeyData) cfrelease (keyData); RetuRN ret;} #pragma mark Delete + (void) Delete: (NSString *) Service {nsmutabledictionary *keychainquery = [Self getkeychainquery:service ]; Secitemdelete ((cfdictionaryref) keychainquery);} @end The relevant release of the modification project is already written in the code comments.
Below is the use of this class
First, we define identifiers for several string types
NSString * Const Key_username_password = @ "Com.company.app.usernamepassword"; NSString * Const KEY_USERNAME = @ "Com.company.app.username"; NSString * Const Key_password = @ "Com.company.app.password";
After that, we create a dictionary and put the user name and password into the dictionary
Nsmutabledictionary *usernamepasswordkvpairs = [nsmutabledictionary dictionary]; [Usernamepasswordkvpairs setobject:@ "UserName" forkey:key_username]; [Usernamepasswordkvpairs setobject:@ "password" Forkey:key_password];
The following methods of referencing the tool class, respectively, the user name and password to add, read, delete operations
A. Write user name and password to KeyChain [KeyChain save:key_username_password data:usernamepasswordkvpairs]; B, read the user name and password from KeyChain nsmutabledictionary *readusernamepassword = (nsmutabledictionary *) [KeyChain Load:key_ Username_password]; NSString *username = [Readusernamepassword objectforkey:key_username]; NSString *password = [Readusernamepassword Objectforkey:key_password]; NSLog (@ "username =%@", username); NSLog (@ "password =%@", password); C, remove the user name and password from KeyChain [KeyChain Delete:key_username_password];
There are many uses for keychain, and we are simply saving the username and password in keychain, not the database or nsuserdefaults, to add security.
iOS development, using keychain to save user name passwords