Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Keychain in iOS actually work?

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?

like image 498
Mikel Avatar asked Aug 12 '11 15:08

Mikel


1 Answers

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!

like image 111
Thomas K Avatar answered Sep 28 '22 00:09

Thomas K