对KeyChain的理解,可参照http://my.oschina.net/w11h22j33/blog/206713
文中利用KeyChain保存用户名和密码,用户名放在属性中,密码放在密码项中;其中对KeyChain项的增删改查,我个人觉得是有问题的,为了方面自己代码上的使用,也帮助阅读该文的其他读者更好的理解,我修正了代码如下:
</pre><p></p><p><pre name="code" class="objc">//存储/增加 -(IBAction)saveClicked:(id)sender{ if(_nameTF.text.length > 0 && _psdTF.text.length > 0) { NSMutableDictionary* dic = [NSMutableDictionary dictionary]; [dic setObject:(__bridge_transfer id)kSecClassGenericPassword forKey:(__bridge_transfer id)kSecClass]; [dic setObject:_nameTF.text forKey:(__bridge_transfer id)kSecAttrAccount]; [dic setObject:[_psdTF.text dataUsingEncoding:NSUTF8StringEncoding] forKey:(__bridge_transfer id)kSecValueData]; OSStatus s = SecItemAdd((__bridge_retained CFDictionaryRef)dic, NULL); NSLog(@"%d",s); } } //获取/查询 -(IBAction)showClicked:(id)sender { if(_nameTF.text.length > 0) { NSDictionary* query = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge_transfer id)kSecClassGenericPassword, (__bridge_transfer id)kSecClass, _nameTF.text, (__bridge_transfer id)kSecAttrAccount, kCFBooleanTrue, kSecReturnAttributes,nil]; CFTypeRef result = nil; OSStatus s = SecItemCopyMatching((__bridge_retained CFDictionaryRef)query, &result); NSLog(@"%d",s); NSLog(@"%@",result); if(s == noErr) {//通过用户名判断item项是否存在 NSMutableDictionary* dic = [NSMutableDictionary dictionary]; [dic setObject:_nameTF.text forKey: (__bridge_transfer id)kSecAttrAccount]; [dic setObject:(__bridge_transfer id)kCFBooleanTrue forKey:(__bridge_transfer id)kSecReturnData]; [dic setObject:(__bridge_transfer id)kSecClassGenericPassword forKey:(__bridge_transfer id)kSecClass]; CFDataRef dataRef = NULL; if(SecItemCopyMatching((__bridge_retained CFDictionaryRef)dic, (CFTypeRef*)&dataRef) == noErr) { NSData *data = (__bridge_transfer id)dataRef; NSLog(@"%ld", data.length); NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); } } }else{ NSDictionary* query = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge_transfer id)kSecClassGenericPassword,(__bridge_transfer id)kSecClass, (__bridge_transfer id)kSecMatchLimitAll, (__bridge_transfer id)kSecMatchLimit, (__bridge_transfer id)kCFBooleanTrue,(__bridge_transfer id)kSecReturnAttributes,nil]; CFArrayRef result = NULL; OSStatus s = SecItemCopyMatching((__bridge_retained CFDictionaryRef)query, (CFTypeRef*)&result); NSArray *array = (__bridge_transfer NSArray*)result;//(可以遍历所有keychain项) NSLog(@"%d",s); NSLog(@"%@",result); } } //删除 -(IBAction)delClicked:(id)sender{ if(_nameTF.text.length > 0){ NSDictionary* query = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge_transfer id)kSecClassGenericPassword,kSecClass, _nameTF.text,kSecAttrAccount,nil]; OSStatus status = SecItemDelete((__bridge_retained CFDictionaryRef)query); NSLog(@"%d",status); } } //改 -(IBAction)modifyClicked:(id)sender{ if(_nameTF.text.length > 0 && _psdTF.text.length > 0) { NSDictionary* query = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge_transfer id)kSecClassGenericPassword, (__bridge_transfer id)kSecClass, _nameTF.text, (__bridge_transfer id)kSecAttrAccount, (__bridge_transfer id)kCFBooleanTrue, (__bridge_transfer id)kSecReturnAttributes,nil]; CFTypeRef result = nil; if(SecItemCopyMatching((__bridge_retained CFDictionaryRef)query, &result) == noErr)//存在才修改 { //设置需要更新的属性列表,一定要删除kSecClass,因为不能update不该字段,否则会报错 NSMutableDictionary* update = [[NSMutableDictionary alloc]init]; [update setObject:[_psdTF.text dataUsingEncoding:NSUTF8StringEncoding] forKey:(__bridge_transfer id)kSecValueData]; [update removeObjectForKey:(__bridge_transfer id)kSecClass]; //设置查找条件,即根据这个条件找到keychain项 NSMutableDictionary* updateItem = [[NSMutableDictionary alloc]init]; [updateItem setObject:[query objectForKey:(__bridge_transfer id)kSecClass] forKey:(__bridge_transfer id)kSecClass]; [updateItem setObject:_nameTF.text forKey:(__bridge_transfer id)kSecAttrAccount]; //参数一表示要查虚的,参数二表示更新后的值 OSStatus status = SecItemUpdate((__bridge_retained CFDictionaryRef)updateItem, (__bridge_retained CFDictionaryRef)update); NSLog(@"%d",status); } } }