I'm using the apple KEychainItemWrapper.The keychain returns values for the first time (and also as long as the app is in active or in background state), but the keychain is returning nil when the app is quit and opened again.
I tested this on both simulator and on device.I'm using iOS 10.2.1 and xcode 8.2.1
Keychain sharing is enabled in my project and the entitlements are correct. I am not sure if I really need to turn on the keychain sharing. It didn't make any difference by turning off the keychain sharing.I still expereince the same behaviour with keychain sharing turned off.
Here is how I am saving and retriving the values:
-(void)saveUSerInfoInKeychain:(NSString *)usrName andPAssworD:(NSString *)password{
_keychain = [[KeychainItemWrapper alloc]initWithIdentifier:[[NSBundle mainBundle]bundleIdentifier] accessGroup:nil];
//also tried setting the accessible type
[_keychain setObject:(__bridge id)kSecAttrAccessibleAlways forKey:(__bridge id)kSecAttrAccessible];
//also set the accessgroup
[_keychain setObject:@"key.app.bndle.identifier"
forKey:(id)kSecAttrAccessGroup];
[_keychain setObject:usrName forKey:(id)kSecAttrAccount];
[_keychain setObject:password forKey:(id)kSecValueData];
}
-(NSString *)getUserEmail{
if (!_keychain) {
_keychain = [[KeychainItemWrapper alloc]initWithIdentifier:[[NSBundle mainBundle]bundleIdentifier] accessGroup:nil];
}
return [_keychain objectForKey:(id)kSecAttrAccount];
}
-(NSString *)getUserPassword{
if (!_keychain) {
_keychain = [[KeychainItemWrapper alloc]initWithIdentifier:[[NSBundle mainBundle]bundleIdentifier] accessGroup:nil];
}
return [_keychain objectForKey:(id)kSecValueData];
}
What am I doing wrong ? Can I init keychain with same identifier multiple times ?