OGeek|极客世界-中国程序员成长平台

标题: ios - 如何从钥匙串(keychain)中删除 nsdictionary 以进行替换 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:38
标题: ios - 如何从钥匙串(keychain)中删除 nsdictionary 以进行替换

我正在将序列化的 nsdictionary 存储并加载到钥匙串(keychain)中,如本文 (Store NSDictionary in 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

值不会从钥匙串(keychain)中删除。

谢谢



Best Answer-推荐答案


这是一个奇怪的地方来存储数据。您将其放入未加密的 kSecAttrService 中。我想你的意思是把它放在 kSecValueData 中(这是唯一被加密的钥匙串(keychain)项)。

也就是说,没有必要删除该项目。您可以随时使用 [keychain setObject:forKey:] 来更新值。 KeychainItemWrapper 自动检测项目是否已经存在,如果存在则更新。

如果要使用 KeychainItemWrapper 删除项目,请使用 -resetKeychainItem。这会使用正确的值调用 SecItemDelete()。如果没有很好地理解 Keychain API 以及 KeychainItemWrapper 的确切方式,您通常不能混合搭配使用 KeychainItemWrapper 和对 SecItem* 的原始调用有效。

关于ios - 如何从钥匙串(keychain)中删除 nsdictionary 以进行替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16174753/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4