是否可以使用NSDictionary(或不使用)将iPhone存储在KeychainItemWrapper钥匙串(keychain)中?
如果不可能,您还有其他解决方案吗?

最佳答案

您必须先将NSDictionary正确序列化,然后再将其存储到钥匙串(keychain)中。
使用:

[dic description]
[dic propertyList]

您将最终得到仅NSDictionary对象的NSString集合。如果要维护对象的数据类型,可以使用NSPropertyListSerialization
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:kSecValueData];

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

if (error) {
    NSLog(@"%@", error);
}

第二次调用NSDictionary返回的NSPropertyListSerialization将保留NSDictionary集合内的原始数据类型。

关于ios - 将NSDictionary存储在钥匙串(keychain)中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9948698/

10-11 03:31
查看更多