iOS development typically uses Userdefaults to access app configuration information, but if you need to save sensitive information, consider using keychain to access it. Development should be noted that the app uninstall after the Userdefaults content will be deleted, and keychain inside the will not, if the first time to write content to keychain, you need to determine whether the keychain contains no previously deleted clean content.
Keychain (keychain) is a password management system in Apple's Mac OS, and iOS has been introduced into iOS as an important tool for user password management. Using Keychain for developers, you can omit the process of encrypting and decrypting sensitive information and leave it to the system management.
It is convenient to use Apple's official keychainitemwrapper or sfhfkeychainutils.
Apple already has a ready-made class that encapsulates keychain,keychainitemwrapper.h and keychainitemwrapper.m files and can be found in generickeychain instances.
The use of Keychain is also simple, and Apple provides a sample code containing a wrapper kit for the keychain operation Keychainitemwrapper. Make the operation of keychain as simple as nsdictionary
The code is as follows |
Copy Code |
Keychainitemwrapp ER *keychainitem = [[Keychainitemwrapper alloc] initwithidentifier:@ "Identifier" accessgroup:@ "Cn.limc.app.boundle"]autorelease]; //Get content in keychain NSString *value = [Keychainitem objectforkey:keyforvalue]; //write content to Keychain NSString *valuetowrite = @ "value to store"; [Keychainitem setobject:valuetowrite forkey:keyforvalue]; |
The following is the use of keychain to access user names and passwords.
The code is as follows |
Copy Code |
CHKeychain.h #import <Foundation/Foundation.h> #import <Security/Security.h> @interface Chkeychain:nsobject + (void) Save: (NSString *) service data: (ID) data; + (ID) Load: (NSString *) service; + (void) Delete: (NSString *) service; @end Chkeychain.m #import "CHKeychain.h" @implementation Chkeychain + (nsmutabledictionary *) Getkeychainquery: (NSString *) Service { Return [nsmutabledictionary Dictionarywithobjectsandkeys: (ID) Ksecclassgenericpassword, (ID) ksecclass, service, (ID) ksecattrservice, service, (ID) ksecattraccount, (ID) ksecattraccessibleafterfirstunlock, (ID) ksecattraccessible, Nil]; } + (void) Save: (NSString *) service data: (ID) Data { Get Search Dictionary Nsmutabledictionary *keychainquery = [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 Archiveddatawithrootobject:data] Forkey: (ID) kSecValueData]; ADD item to keychain with the search dictionary Secitemadd ((cfdictionaryref) keychainquery, NULL); } + (ID) Load: (NSString *) Service { ID ret = NIL; Nsmutabledictionary *keychainquery = [self getkeychainquery:service]; Configure the search setting Since in my simple case we are expecting only a, 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 { ret = [Nskeyedunarchiver unarchiveobjectwithdata: (NSData *) KeyData]; } @catch (NSException *e) { NSLog (@ "Unarchive of%@ failed:%@", service, E); } @finally { } } if (KeyData) Cfrelease (KeyData); return ret; } + (void) Delete: (NSString *) Service { Nsmutabledictionary *keychainquery = [self getkeychainquery:service]; Secitemdelete ((cfdictionaryref) keychainquery); } @end |
First you need to define a few strings to do the key:
The code is as follows |
Copy Code |
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"; |
Deposit username and password into keychain:
The code is as follows |
Copy Code |
Nsmutabledictionary *usernamepasswordkvpairs = [Nsmutabledictionary dictionary]; [Usernamepasswordkvpairs SetObject:txtfldUsername.text Forkey:key_username]; [Usernamepasswordkvpairs SetObject:txtfldPassword.text Forkey:key_password]; [Chkeychain Save:key_username_password Data:usernamepasswordkvpairs];
|
To remove a user name and password from the keychain:
The code is as follows |
Copy Code |
Nsmutabledictionary *usernamepasswordkvpairs = (nsmutabledictionary *) [Chkeychain Load:key_username_password]; Txtfldusername.text = [Usernamepasswordkvpairs objectforkey:key_username]; Txtfldpassword.text = [Usernamepasswordkvpairs Objectforkey:key_password]; |
Delete a keychain item:
The code is as follows |
Copy Code |
[CPP] View plaincopyprint? 01.[chkeychain Delete:key_username_password]; [Chkeychain Delete:key_username_password];
|
Such a simple function of using keychain to access the username password is done.