I've been trying to use the keychain in iOS for storing some small bits of information — password strings, OAuth tokens, etc. I'm using the KeychainItemWrapper sample code that Apple provides here: https://developer.apple.com/library/ios/#samplecode/GenericKeychain/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007797
I've found it extremely buggy! Sometimes it works, other times my app crashes when trying to store string values in the keychain, particularly when something was already set. Other times, the exact same calls work just fine. The errors happen on actual devices, not in the simulator.
The way I typically write to the keychain is like this:
KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"password" accessGroup:nil];
[wrapper setObject:thePasswordString forKey:(id)kSecValueData];
[wrapper release];
So, what I would like to know is: am I doing something wrong, is the sample code from Apple to blame, or is the actual underlying iOS keychain API broken?
I've found SFHFKeychainUtilities to be an extremely helpful wrapper. It provides a very simple API that looks like this:
[SFHFKeychainUtils storeUsername:usernameInput andPassword:passwordInput forServiceName:@"foo" updateExisting:TRUE error:&error];
Here's a useful tutorial: http://gorgando.com/blog/tag/sfhfkeychainutils
Works all the time for me.
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With