是否可以使用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/