IOS KeyChain comprehension and code examples
Understanding of KeyChain, refer to http://my.oschina.net/w11h22j33/blog/206713
In this article, the KeyChain is used to save the user name and password. The user name is placed in the attribute and the password is placed in the password item. I personally think there is a problem with the addition, deletion, modification, and query of the KeyChain item, in order to use your own code and help other readers who read this article better understand it, I corrected the Code as follows:
// Storage/Add-(IBAction) saveClicked :( id) sender {if (_ nameTF. text. length> 0 & _ psdTF. text. length> 0) {NSMutableDictionary * dic = [NSMutableDictionary dictionary]; [dic setObject :( _ bridge_transfer id) kSecClassGenericPassword forKey :( _ partition 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 (_ effeccfdictionaryref) dic, NULL); NSLog (@ % d, s) ;}}// get/query-(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, delimiter, nil]; CFTypeRef result = nil; OSStatus s = SecItemCopyMatching (_ effeccfdictionaryref) query, & result ); NSLog (@ % d, s); NSLog (@ % @, result); if (s = noErr) {// use the user name to determine whether the item exists 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 :( _ partition id) kSecClassGenericPassword forKey :( _ bridge_transfer id) kSecClass]; CFDataRef dataRef = NULL; if (SecItemCopyMatching (_ partition 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: (_ partition id) kSecClassGenericPassword, (_ bridge_transfer id) kSecClass, (_ partition id) kSecMatchLimitAll, (_ partition id) kSecMatchLimit, (_ partition id) kCFBooleanTrue, (_ bridge_transfer id) kSecReturnAttributes, nil]; CFArrayRef result = NULL; OSStatus s = SecItemCopyMatching (_ effeccfdictionaryref) query, (CFTypeRef *) & result ); NSArray * array = (_ bridge_transfer NSArray *) result; // (all keychain items can be traversed) NSLog (@ % d, s); NSLog (@ % @, result) ;}}// Delete-(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) ;}/// modify-(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) delimiter, nil]; CFTypeRef result = nil; if (SecItemCopyMatching (_ bridge_retained CFDictionaryRef) query, & result) = noErr) // The attribute list to be updated must be deleted before modification, because the field cannot be updated, otherwise NSMutableDictionary * update = [[NSMutableDictionary alloc] init]; [update setObject: [_ psdTF. text dataUsingEncoding: NSUTF8StringEncoding] forKey :( _ bridge_transfer id) kSecValueData]; [update removeObjectForKey :( _ bridge_transfer id) kSecClass]; // you can specify search conditions, that is, find the keychain item 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]; // parameter 1 indicates the virtual query, parameter 2 indicates the updated value OSStatus status = SecItemUpdate (_ bridge_retained CFDictionaryRef) updateItem, (_ bridge_retained CFDictionaryRef) update); NSLog (@ % d, status );}}}