如何利用钥匙串存储App的账号信息,废话不多说直接上代码
.h
@interface PRZKeychainTool : NSObject
/**
* 储存字符串到🔑钥匙串
*
* @param sValue 对应的Value
* @param sKey 对应的Key
*/
+ (void)saveKeychainValue:(NSString *)sValue key:(NSString *)sKey;
/**
* 从🔑钥匙串获取字符串
*
* @param sKey 对应的Key
*
* @return 返回储存的Value
*/
+ (NSString *)readKeychainValue:(NSString *)sKey;
/**
* 从🔑钥匙串删除字符串
*
* @param sKey 对应的Key
*/
+ (void)deleteKeychainValue:(NSString *)sKey;
@end
.m
@implementation PRZKeychainTool
+ (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)saveKeychainValue:(NSString *)sValue key:(NSString *)sKey{
NSMutableDictionary * keychainQuery = [self getKeychainQuery:sKey];
SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery);
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:sValue] forKey:(__bridge_transfer id)kSecValueData];
SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, NULL);
}
// 从🔑钥匙串获取字符串
+ (NSString *)readKeychainValue:(NSString *)sKey
{
NSString *ret = nil;
NSMutableDictionary *keychainQuery = [self getKeychainQuery:sKey];
[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 CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
@try {
ret = (NSString *)[NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
} @catch (NSException *e) {
NSLog(@"Unarchive of %@ failed: %@", sKey, e);
} @finally {
}
}
if (keyData)
CFRelease(keyData);
return ret;
}
// 从🔑钥匙串删除字符串
+ (void)deleteKeychainValue:(NSString *)sKey {
NSMutableDictionary *keychainQuery = [self getKeychainQuery:sKey];
SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
}
@end
使用
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *oneTF;
@property (weak, nonatomic) IBOutlet UITextField *twoTF;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
//读取
- (IBAction)duqu:(id)sender {
self.oneTF.text = [NSString stringWithFormat:@"读取到用户名:%@",[PRZKeychainTool readKeychainValue:@"uaneName"]];
self.twoTF.text = [NSString stringWithFormat:@"读取到用密码:%@",[PRZKeychainTool readKeychainValue:@"paw"]];
}
//保存
- (IBAction)baocun:(id)sender {
[PRZKeychainTool saveKeychainValue:self.oneTF.text key:@"uaneName"];
[PRZKeychainTool saveKeychainValue:self.twoTF.text key:@"paw"];
}
//删除
- (IBAction)shanchu:(id)sender {
[PRZKeychainTool deleteKeychainValue:@"uaneName"];
[PRZKeychainTool deleteKeychainValue:@"paw"];
}
![Uploading 645977-20151224142516359-146623890_193643.png . . .]##一、APP对钥匙串的访问权限:
(1)未对应用APP的entitlement(授权)进行配置时,APP使用钥匙串存储时,会默认存储在自身BundleID的条目下。
本人新手呆鸟,忘各位老司机多多鞭策,使我快速成长。谢啦
网友评论