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 > && 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.效果图
参考文 http://blog.csdn.net/yiyaaixuexi/article/details/7688745