In general, we use Nsuserdefaults to store data information, but for some private information, such as passwords, certificates, and so on, we need to use a more secure keychain. The information stored in the keychain will not be lost due to the deletion of the app, still valid after the user re-installs the app, and the data is still available.
Using Apple's official release of the Keychainitemwrapper or sfhfkeychainutils is very convenient, and later saw the iphone using Keychain to access the user name and password article, I feel that the understanding of keychain is very helpful, So the arc control also tried a piece.
Need to import Security.framework
@implementation Wqkeychain
+ (Nsmutabledictionary *) Getkeychainquery: (NSString *) Service {
return [nsmutabledictionary Dictionarywithobjectsandkeys:
(__bridge_transfer ID) Ksecclassgenericpassword, (__bridge_transfer ID) ksecclass,
Service, (__bridge_transfer ID) ksecattrservice,
Service, (__bridge_transfer ID) ksecattraccount,
(__bridge_transfer ID) ksecattraccessibleafterfirstunlock, (__bridge_transfer 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 ((__bridge_retained cfdictionaryref) keychainquery);
Add new object to search dictionary (attention:the data format)
[Keychainquery setobject:[nskeyedarchiver Archiveddatawithrootobject:data] Forkey: (__bridge_transfer ID) Ksecvaluedata];
ADD item to keychain with the search dictionary
Secitemadd ((__bridge_retained cfdictionaryref) keychainquery, NULL);
}
+ (ID) Load: (NSString *) Service {
ID ret = NIL;
Nsmutabledictionary *keychainquery = [self getkeychainquery:service];
Configure the search setting
[Keychainquery setobject: (ID) kcfbooleantrue Forkey: (__bridge_transfer ID) ksecreturndata];
[Keychainquery setobject: (__bridge_transfer ID) ksecmatchlimitone forkey: (__bridge_transfer ID) kSecMatchLimit];
Cfdataref keyData = NULL;
if (secitemcopymatching (__bridge_retained cfdictionaryref) keychainquery, (Cftyperef *) &keydata) = = NOERR) {
@try {
ret = [Nskeyedunarchiver unarchiveobjectwithdata: (__bridge_transfer NSData *) KeyData];
} @catch (NSException *e) {
NSLog (@ "Unarchive of%@ failed:%@", service, E);
} @finally {
}
}
return ret;
}
+ (void) Delete: (NSString *) Service {
Nsmutabledictionary *keychainquery = [self getkeychainquery:service];
Secitemdelete ((__bridge_retained cfdictionaryref) keychainquery);
}
@endFor example, save the password@interface Wquserdatamanager:nsobject
/**
* @brief Store password
*
* @param password Password content
*/
+ (void) Savepassword: (NSString *) password;
/**
* @brief Read password
*
* @return Password content
*/
+ (ID) Readpassword;
/**
* @brief Delete password data
*/
+ (void) Deletepassword;
@end
#import "WQUserDataManager.h"
@implementation Wquserdatamanager
Static NSString * Const Key_in_keychain = @ "Com.wuqian.app.allinfo";
Static NSString * Const KEY_PASSWORD = @ "Com.wuqian.app.password";
+ (void) Savepassword: (NSString *) password
{
Nsmutabledictionary *usernamepasswordkvpairs = [Nsmutabledictionary dictionary];
[Usernamepasswordkvpairs Setobject:password Forkey:key_password];
[Wqkeychain Save:key_in_keychain Data:usernamepasswordkvpairs];
}
+ (ID) readpassword
{
Nsmutabledictionary *usernamepasswordkvpair = (nsmutabledictionary *) [Wqkeychain Load:key_in_keychain];
return [Usernamepasswordkvpair Objectforkey:key_password];
}
+ (void) Deletepassword
{
[Wqkeychain Delete:key_in_keychain];
}
@endImplement a simple interface, save the set password, and then immediately read the display to see the effect-(ibaction) Btnaciton: (ID) sender
{
[Wquserdatamanager SavePassWord:self.textfield.text];
Self.label.text = [Wquserdatamanager Readpassword];
}
achieved the desired effect.
Use of Keychain