IOS KeyChain理解及代码事例
来源:互联网 发布:守望先锋亚服淘宝 编辑:程序博客网 时间:2024/04/30 06:21
对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); } }}
0 0
- IOS KeyChain理解及代码事例
- MVC 精髓理解事例代码
- iOS - Keychain保存用户帐号及密码
- ios keyChain
- IOS KeyChain
- IOS KeyChain
- IOS KeyChain
- iOS KeyChain
- iOS KeyChain
- IOS KeyChain
- IOS KeyChain
- IOS KeyChain
- iOS - KeyChain
- 代码事例
- iOS 的keyChain
- iOS 的keyChain
- 关于iOS keychain
- iOS 的keyChain
- Android GridView的使用
- H.264 I帧 P帧 B帧
- yum安装svn服务器
- 举例说明对象和类
- iOS 万能跳转界面方法 (runtime实用篇一)
- IOS KeyChain理解及代码事例
- oracle pctfree和pctused详解
- Android soap请求和解析
- 第二坑
- hdoj 4062 Queuing 【矩阵快速幂优化递推公式】
- 在ArcGIS中创建Python工具(二)
- C++类的空间大小
- Android学习笔记---SQLite数据库 api
- WIndows 发邮件