The iOS Keychain service provides a secure way to save private information (passwords, serial numbers, certificates, and so on). Each iOS program has a separate keychain store. Starting with iOS 3.0, it becomes possible to share keychain across programs.
The following is the use of keychain to implement access to user names and passwords.
Apple already has ready-made classes to encapsulate the keychain,keychainitemwrapper.h and keychainitemwrapper.m files, which can be found in the Generickeychain instance.
But here I just need to access the user name and password, do not use the class provided by Apple, write a simple class to implement it.
The code is as follows:
CHKeychain.h
#import #import @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 Archiveddata Withrootobject: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 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;} + (void) Delete: (NSString *) Service {nsmutabledictionary *keychainquery = [self getkeychainquery:service]; Secitemdelete ((cfdictionaryref) keychainquery);} @end
You first need to define several strings to do the key:
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";
To deposit the username and password in keychain:
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];
Remove the user name and password from the keychain:
Nsmutabledictionary *usernamepasswordkvpairs = (nsmutabledictionary *) [Chkeychain Load:key_username_password]; Txtfldusername.text = [Usernamepasswordkvpairs objectforkey:key_username];txtfldpassword.text = [ Usernamepasswordkvpairs Objectforkey:key_password];
Delete a keychain item:
[Chkeychain Delete:key_username_password];
Such a simple use of Keychain Access user name password function is done.
Also enclosed is a more detailed e-article: http://useyourloaf.com/blog/2010/3/29/simple-iphone-keychain-access.html
iOS development--The use of keychain for password storage