繁体   English   中英

如何从钥匙串中删除nsdictionary以进行替换

[英]how to remove nsdictionary from keychain for replacing

我正在像本文中那样存储序列化的nsdictionary并将其加载到钥匙串中(将NSDictionary存储在keychain中 ),但是我需要能够更新/编辑字典内容,因此我希望将其删除并重新添加。

我只是不知道该怎么做。 我从上面的帖子中获取了以下代码:

    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

从钥匙串中删除的值。

谢谢

这是一个奇怪的存储数据的地方。 您将其放入未加密的kSecAttrService 我认为您的意思是将其放入kSecValueData (这是加密的钥匙串项中唯一的一块)。

也就是说,无需删除该项目。 您可以随时使用[keychain setObject:forKey:]更新值。 KeychainItemWrapper自动检测该项目是否已经存在,并进行更新。

如果要使用KeychainItemWrapper删除项目,请使用-resetKeychainItem 这将使用正确的值调用SecItemDelete() 在没有充分了解Keychain API以及KeychainItemWrapper工作原理的情况下,通常不能混合使用KeychainItemWrapper和对SecItem*原始调用。

虽然我从未尝试访问钥匙串属性。 但是通过查看Apple文档中的SecItemDelete方法,预期的参数是字典,在您的代码中,您正在传递NSData类型的dictionaryRep

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

在这里找到了一个问题,希望对您有所帮助。

我想在Mac OS X上删除自己创建的KeyChain中的所有项目

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM