Search code examples
ioskeychainkeychainitemwrapper

iOS Remove Keychain Value


I'm using the Keychain like so:

KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"LoginData" accessGroup:nil];
[keychain setObject:responseObject[@"TOK"] forKey:CFBridgingRelease(kSecAttrAccount)];

And wish to remove (null the value) like so:

[keychain setValue:nil forKey:CFBridgingRelease(kSecAttrAccount)];

However, i only see this:

setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key acct.

Like wise, when i use this:

[keychain setNilValueForKey:CFBridgingRelease(kSecAttrAccount)];

I get this:

setNilValueForKey]: could not set nil as the value for the key acct.

I'm using apple's KeychainItemWrapper, How would i do this correctly?


Solution

  • In general to delete an item, generate the query you will normally do to fetch it, then use "SecItemDelete".

    Like this -

        NSMutableDictionary *query = [self getQueryForKey:key];
    OSStatus status = SecItemDelete((__bridge CFDictionaryRef)query);
    if( status != errSecSuccess) {
        ...
    }
    

    If you're using the keyChainWrapper you can do -

        KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"LoginData" accessGroup:nil];
        [keychain resetKeychainItem];