首页 > 解决方案 > saving in ios keychain is not persistent

问题描述

I am using Apple KeyChainItemWrapper to store to keychain.

The problem is if I log the stored data directly after saving (by instantiate a new keychainItem with the same identifier) I can see the data. But when I kill the app and reopen it it returns nil.

This is how i save to keychain:

KeychainItemWrapper* keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"test" accessGroup:nil];



NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

[dict setObject:@{@"email": @"example@aol.com"} forKey:@"data"];

NSData *dictionaryRep = [NSPropertyListSerialization dict format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];
NSLog(@"NSPropertyListSerialization error: %@", error);


[keychain setObject:@"test" forKey:(id)kSecAttrAccount];
[keychain setObject:(__bridge id)(kSecAttrAccessibleAlwaysThisDeviceOnly) forKey:(__bridge id)(kSecAttrAccessible)];
[keychain setObject:dictionaryRep forKey:(id)kSecValueData];

and this is how i access the value:

KeychainItemWrapper* keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"test" accessGroup:nil];
NSData *infoData = [keychain objectForKey:(id)kSecValueData];
NSError* error;
NSDictionary *infoDictionary = [NSPropertyListSerialization propertyListWithData:infoData options:NSPropertyListImmutable format:nil error:&error];
NSLog(@"KeychainItemWrapper test : %@", infoDictionary);

Thank you for your help

标签: ioskeychainkeychainitemwrapper

解决方案


推荐阅读