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 blam开发者_运维技巧e, 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!
I had many problems with this Wrapper. I don't know why, but you must have to save to KSecAttrAccount with the same identifier, and before to save the kSecValueData.
Save like this:
KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"MyIdentifier" accessGroup:nil];
[keychain setObject:@"MyIdentifier" forKey:(id)kSecAttrAccount];
[keychain setObject:[passwordFirst text] forKey:(id)kSecValueData];
The KeychainItemWrapper
from Apple does indeed work - I'm using it in one of my apps to store IAP data. What I have found, however, is that it doesn't always seem to work if your device is jailbroken. Not sure what happens to the keychain on when a device is jailbroken, but some of my beta testers have reported that the IAPs don't always apply when their device is jailbroken.
You seem to be using the class properly.
精彩评论