保存密码(KeyChain的使用)

1.导入框架Security.framework

保存密码(KeyChain的使用)

2.编写工具类

/* 该工具类只能保存一个用户和密码  */
/* service 一般为 bundle ID */
@interface GLKeyChainManage : NSObject
+(void)save:(NSString *)service data:(id)data;
+(id)load:(NSString *)service;
+(void)deleted:(NSString *)service; @end
@implementation GLKeyChainManage

+(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{
NSMutableDictionary *keyChainQuery = [self getKeyChainQuery:service]; SecItemDelete((__bridge_retained CFDictionaryRef)keyChainQuery); [keyChainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge_transfer id)kSecValueData]; SecItemAdd((__bridge_retained CFDictionaryRef)keyChainQuery, NULL);
} +(id)load:(NSString *)service{
id ret = nil;
NSMutableDictionary *keyChainQuery = [self getKeyChainQuery:service]; [keyChainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge_transfer id)kSecReturnData];
[keyChainQuery setObject:(__bridge_transfer id)kSecMatchLimitOne forKey:(__bridge_transfer id)kSecMatchLimit]; CFDataRef keyData = nil;
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)deleted:(NSString *)service{
NSMutableDictionary *keyChainQuery = [self getKeyChainQuery:service];
SecItemDelete((__bridge_retained CFDictionaryRef)keyChainQuery);
} @end

3.使用场景

#import "HGLKeyChainManage.h"

@interface ViewController ()<UITextFieldDelegate>
{
NSString *bundleID;
}
@property (weak, nonatomic) IBOutlet UITextField *usernameLabel;
@property (weak, nonatomic) IBOutlet UITextField *passwordLabel; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
bundleID = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)deleteKey:(id)sender {
[HGLKeyChainManage deleted:bundleID];
}
- (IBAction)saveKey:(id)sender {
NSString *username = self.usernameLabel.text;
NSString *password = self.passwordLabel.text;
if (username.length > && password.length > ) {
NSMutableDictionary *usernamepasswordKVPairs = [NSMutableDictionary dictionary];
[usernamepasswordKVPairs setObject:password forKey:username];
[HGLKeyChainManage save:bundleID data:usernamepasswordKVPairs];
} }
- (IBAction)showSecure:(id)sender { ((UIButton *)sender).selected = _passwordLabel.secureTextEntry;
_passwordLabel.secureTextEntry = !_passwordLabel.secureTextEntry;
} #pragma mark - UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if (textField == self.usernameLabel) {
[textField resignFirstResponder];
NSMutableDictionary *usernamepasswordKVPair = (NSMutableDictionary *)[HGLKeyChainManage load:bundleID];
NSString *password = [usernamepasswordKVPair objectForKey:textField.text];
if (password != nil) {
_passwordLabel.text = password;
}else{
[_passwordLabel becomeFirstResponder];
}
}else{
[textField resignFirstResponder];
}
return YES;
} @end

4.效果图

保存密码(KeyChain的使用)

参考文  http://blog.csdn.net/yiyaaixuexi/article/details/7688745

上一篇:mysql 5.7以上版本安装配置方法图文教程(mysql 5.7.12\mysql 5.7.13\mysql 5.7.14)(转)


下一篇:Spring Boot -05- 多模块结构项目构建与测试(详细图文教程)IDEA 版