1
votes

I've been looking all around but still not found the answer. Any idea how to return all kSecAttrAccounts from keychain? I would like to get back a list of every identifier I used when creating keychain items, then to choose the one I want to delete it using a "[KeychainWrapper deleteItemFromKeychainWithIdentifier:identifier] method".

It works fine when I remember the username (identifier) I used when creating the account but cannot figure out how to have them all back when you created lots of them.

I tried a kind of basic [dictionary objectForKey:(__bridge(id)kSecAttrAccount)] but it does not make it.

Thanks a lot!!

Tricho

1

1 Answers

1
votes

Use kSecMatchLimitAll to get all values in your query dictionary for kSecMatchLimit

 (__bridge id)kSecMatchLimitAll, (__bridge id)kSecMatchLimit 

It will fetch all passwords in keychain for kSecClassGenericPassword.You can use other keychain classes as well like this

NSMutableDictionary *query = [NSMutableDictionary dictionaryWithObjectsAndKeys:
    (__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnAttributes,
    (__bridge id)kSecMatchLimitAll, (__bridge id)kSecMatchLimit,
    (__bridge id)kSecClassGenericPassword, (__bridge id)kSecClass, //change your class in query
    nil];

    CFTypeRef result = NULL;
    SecItemCopyMatching((__bridge CFDictionaryRef)query, &result);
    NSLog(@"%@", (__bridge id)result);
    if (result != NULL) CFRelease(result);

EDIT : For delting all keys of your app you can use

+(void)deleteAllKeychainItems{


    NSArray *secItemClasses = @[(__bridge id)kSecClassGenericPassword,
                                (__bridge id)kSecClassInternetPassword,
                                (__bridge id)kSecClassCertificate,
                                (__bridge id)kSecClassKey,
                                (__bridge id)kSecClassIdentity];
    for (id secItemClass in secItemClasses) {
        NSDictionary *spec = @{(__bridge id)kSecClass:secItemClass};
        SecItemDelete((__bridge CFDictionaryRef)spec);
    }

}

It will delete all keychain items including all accounts password or values associated.