码迷,mamicode.com
首页 > 其他好文 > 详细

保存密码(KeyChain的使用)

时间:2015-08-09 20:19:56      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:

1.导入框架Security.framework

技术分享

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 > 0 && password.length > 0) {
        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.效果图

技术分享

 

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

保存密码(KeyChain的使用)

标签:

原文地址:http://www.cnblogs.com/gulong/p/4716190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!