9

I stored some information in the keychain, and there is a case that I need to remove all of the items, instead of doing [keychain removeObjectForKey:theKey] for all the keys, can I do:

NSDictionary *spec = [NSDictionary dictionaryWithObjectsAndKeys:(id)kSecClassGenericPassword, kSecClass,
                      [self serviceName], kSecAttrService, nil];

return !SecItemDelete((CFDictionaryRef)spec);

instead?

I tried it and it worked, just not sure if I am doing the correct thing?

2 Answers 2

5

in my app I'm using this line to clear my keychain :

[[[KeychainItemWrapper alloc] initWithIdentifier:@"my_key" accessGroup:nil] resetKeychainItem]
2
  • What is that identifier? Is it the kSecAttrService?
    – hzxu
    Commented Mar 7, 2013 at 4:06
  • You can choose whatever identifier you'd like, in case you need to have multiple KeychainItem handled in your application.
    – Alexis C.
    Commented Mar 8, 2013 at 14:26
0

I believe what you are doing is correct, in fact, you can avoid the kSecAttrService parameter in your query if you want. On the other hand, the SecItemDelete returns a OSStatus value which you can check for more detailed information about the transaction.

    NSDictionary *spec = [NSDictionary dictionaryWithObjectsAndKeys:(id)kSecClassGenericPassword, kSecClass, nil];

    OSStatus status = SecItemDelete((CFDictionaryRef)spec);
    if (status == errSecSuccess)
       return YES;

    return NO;

Here are the codes and meanings for the possible status values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.