封装了一些存储入钥匙串的方法。这个方法与本地存储的区别是什么呢?查看了大部分的文档解读,我觉得就是:
1.不易丢失。如果项目迭代更新、重装等等。存储的本地内容将不会存在。而钥匙串中的数据还能保存。
2.破解难度大。其他的本地存储截取更简单一些,所以用户名密码存储做的好点最好是不要放入本地存储中。加密放入钥匙串更加不易破解。
封装:
1.在Capabilities中勾选Keychain Sharing.
2.创建一个文件用于封装。
这里的封装适合保存一个值。多个值的封装需要传入保存的Key。
3.方法创建:
/**
* 存储字符串到 KeyChain
*
* @param string NSString
*/
+ (void)keyChainSave:(NSString *)string;
/**
* 从 KeyChain 中读取存储的字符串
*
* @return NSString
*/
+ (NSString *)keyChainLoad;
/**
* 删除 KeyChain 信息
*/
+ (void)keyChainDelete;
4.方法实现
static NSString * const kPDDictionaryKey = @"com.xxx.dictionaryKey";
static NSString * const kPDKeyChainKey = @"com.xxx.keychainKey";
+ (void)keyChainSave:(NSString *)string {
NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
[tempDic setObject:string forKey:kPDDictionaryKey];
[self save:kPDKeyChainKey data:tempDic];
}
+ (NSString *)keyChainLoad{
NSMutableDictionary *tempDic = (NSMutableDictionary *)[self load:kPDKeyChainKey];
return [tempDic objectForKey:kPDDictionaryKey];
}
+ (void)keyChainDelete{
[self delete:kPDKeyChainKey];
}
内部方法
+ (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 our simple case we are 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 {
ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge 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);
}
使用:
// 存
- (void)saveUseAccount {
[PDKeyChain keyChainSave:self.userNameTextField.text];
}
// 读取
// 从keychain里取账号信息
self.userNameTextField.text = [PDKeyChain keyChainLoad];
ps:中间内容参考别人的,但是出处没有查到了。只是个人记录以后使用。
网友评论