2013-04-23 53 views
2

我正在存儲和加載序列化的nsdictionary到鑰匙串中(Store NSDictionary in keychain),但我需要能夠更新/編輯字典內容,所以我想刪除它並重新添加。如何從鑰匙串中刪除nsdictionary替換

我只是不知道該怎麼做。我從以上帖子中獲取以下代碼:

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 

這些值不會從鑰匙串中刪除。

謝謝

回答

1

這是存儲數據的一個陌​​生的地方。你把它放在kSecAttrService,這是不加密的。我想你的意思是把它放在kSecValueData(這是加密的鑰匙鏈項目中唯一的一塊)。

也就是說,沒有必要刪除該項目。您可以使用[keychain setObject:forKey:]來隨時更新值。 KeychainItemWrapper會自動檢測項目是否已經存在,如果有,則會更新它。

如果要使用KeychainItemWrapper刪除該項目,請使用-resetKeychainItem。這需要用正確的值調用SecItemDelete()。如果沒有深入瞭解Keychain API以及KeychainItemWrapper的工作原理,您一般不能混合使用KeychainItemWrapper和原始調用SecItem*

0

雖然我從來沒有在訪問Keychain屬性。但通過查看Apple文檔中的SecItemDelete方法,期望的參數是字典,在您的代碼中,您傳遞的是dictionaryRep,這是一個NSData類型。

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

發現了一個問題在這裏,希望它可以幫助。

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