Manu Manu - 2 months ago 16
iOS Question

Reset An iPhone App's Keychain

I would like to know if there is a way to reset My app's keychain. I am wondering whether anything exist like

[NSUserDefaults resetStandardUserDefaults]

for keychain. Key chain is not reset even after the app is deleted. So far the only way I know is to reset them one by one from the app.


As all of the answers so far relies on that you know the identifiers you want to delete I would like to submit the following solution that deletes ALL existing keys for the app (iOS only)

-(void)resetKeychain {
    [self deleteAllKeysForSecClass:kSecClassGenericPassword];
    [self deleteAllKeysForSecClass:kSecClassInternetPassword];
    [self deleteAllKeysForSecClass:kSecClassCertificate];
    [self deleteAllKeysForSecClass:kSecClassKey];
    [self deleteAllKeysForSecClass:kSecClassIdentity];

-(void)deleteAllKeysForSecClass:(CFTypeRef)secClass {
    NSMutableDictionary* dict = [NSMutableDictionary dictionary];
    [dict setObject:(__bridge id)secClass forKey:(__bridge id)kSecClass];
    OSStatus result = SecItemDelete((__bridge CFDictionaryRef) dict);
    NSAssert(result == noErr || result == errSecItemNotFound, @"Error deleting keychain data (%ld)", result);

Swift 2.2 version:

func resetKeychain() {

func deleteAllKeysForSecClass(secClass: CFTypeRef) {
    let dict: [NSString : AnyObject] = [kSecClass : secClass]
    let result = SecItemDelete(dict)
    assert(result == noErr || result == errSecItemNotFound, "Error deleting keychain data (\(result))")