skeg0
skeg0

Reputation: 187

how to remove nsdictionary from keychain for replacing

I am storing and loading a serialized nsdictionary to the keychain as in this post (Store NSDictionary in keychain), but I need to be able to update/edit the dictionary contents so I would like to delete it and re add.

I just dont know how to do it. I have the following code taken from the above post:

    KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"arbitraryId" accessGroup:nil]
    NSString *error;
    //The following NSData object may be stored in the Keychain
    NSData *dictionaryRep = [NSPropertyListSerialization dataFromPropertyList:dictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
    [keychain setObject:dictionaryRep forKey:kSecAttrService];

    //When the NSData object object is retrieved from the Keychain, you convert it back to NSDictionary type
    dictionaryRep = [keychain objectForKey:kSecAttrServce];
    NSDictionary *dictionary = [NSPropertyListSerialization propertyListFromData:dictionaryRep mutabilityOption:NSPropertyListImmutable format:nil errorDescription:&error];

    SecItemDelete((CFDictionaryRef)dictionaryRep); // doesnt work

the values arent deleted from the keychain.

thanks

Upvotes: 2

Views: 1147

Answers (2)

Rob Napier
Rob Napier

Reputation: 299475

This is a strange place to store the data. You're putting it in kSecAttrService, which isn't encrypted. I think you mean to put this in kSecValueData (this is the only piece of a keychain item that is encrypted).

That said, there's no need to delete the item. You can just use [keychain setObject:forKey:] to update the value whenever you want. KeychainItemWrapper automatically detects whether the item exists already, and updates it if it does.

If you want to delete the item using KeychainItemWrapper, use -resetKeychainItem. This calls SecItemDelete() with the correct value. You cannot in general mix-and-match the use of KeychainItemWrapper and raw calls to SecItem* without a good understanding of the Keychain API and exactly how KeychainItemWrapper works.

Upvotes: 1

Deepesh Gairola
Deepesh Gairola

Reputation: 1242

Although i have never worked on accessing Keychain properties. but by looking at the SecItemDelete method in Apple document the expected parameter is a dictionary, in your code you are passing dictionaryRep which is a NSData type.

https://developer.apple.com/library/mac/#documentation/security/Reference/keychainservices/Reference/reference.html#//apple_ref/doc/c_ref/SecItemDelete

Found a question here, hope it might help.

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

Upvotes: 0

Related Questions