Search code examples
objective-cmacoscocoakeychaincore-foundation

I want to delete all items in my self created KeyChain on Mac OS X


I'm writing a little tool to synchronize passwords. I'm using my own KeyChain for this purpose. Prior to saving, I want to clear this KeyChain. However, it seems I don't understand how to use the SecItemDelete function.

NSMutableDictionary *deleteQuery = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                            kSecClassGenericPassword, kSecClass,
                                            kSecMatchLimit, kSecMatchLimitAll, nil];

    OSStatus status = SecItemDelete((__bridge CFDictionaryRef)deleteQuery);
    NSLog(@"%@", SecCopyErrorMessageString(status, NULL));

This is what I've written so far, but unfortunately my items (called Root.Foo and Root.Bar) remain in the KeyChain. Also I'm wondering, how this function knows, which KeyChain should be searched? Most examples I'm fonding are about iOS, where every Application has it own KeyChain by default.

Thanks for any help :)


Solution

  • Solved it:

    I've missed passing in an array of KeyChains to look for! It seems on iOS, always the default KeyChain of an app is used but on Mac OS you need to specify the KeyChain, as an array containing SecKeychainRefs:

    NSMutableDictionary *q = [NSMutableDictionary dictionary];
        [q setObject:kSecClassGenericPassword forKey:kSecClass];
        [q setObject:[NSArray arrayWithObject:(__bridge id)keyChain] forKey:kSecMatchSearchList];
        [q setObject:kSecMatchLimitAll forKey:kSecMatchLimit];
        SecItemDelete((__bridge CFDictionaryRef)q);
    

    This code worked perfectly.