hzxu
hzxu

Reputation: 5823

Correct way to remove(purge) all keychain data for an iOS app

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?

Upvotes: 9

Views: 2753

Answers (2)

Omer
Omer

Reputation: 5600

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

Upvotes: 0

Alexis C.
Alexis C.

Reputation: 4918

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

[[[KeychainItemWrapper alloc] initWithIdentifier:@"my_key" accessGroup:nil] resetKeychainItem]

Upvotes: 5

Related Questions