Does Keychain really work on iOS?

I am trying to use keychain in iOS to store small bits of information - password strings, OAuth tokens, etc. I use 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 found it extremely buggy! Sometimes this works while my application crashes when trying to save string values ​​in a keychain, especially when something is already installed. In other cases, the same calls work fine. Errors occur on real devices, not in the simulator.

I usually write to the keychain as follows:

KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"password" accessGroup:nil];
[wrapper setObject:thePasswordString forKey:(id)kSecValueData];
[wrapper release];

So, I would like to know that I am doing something wrong, is this an example of Apple's code that is to blame, or is the actual basic IOS API for key fobs?

+5
source share
3 answers

I found SFHFKeychainUtilities an extremely useful shell. 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 for me all the time.

Good luck

+4
source

I had a lot of problems with this Wrapper. I do not know why, but you need to save in KSecAttrAccount with the same identifier, and then save kSecValueData.

Save the following:

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

[keychain setObject:@"MyIdentifier" forKey:(id)kSecAttrAccount];

[keychain setObject:[passwordFirst text] forKey:(id)kSecValueData];
+3
source

KeychainItemWrapperApple really works - I use it in one of my IAP storage applications. However, I found that it does not always work if your device is locked. I'm not sure what happens with key fobs when the device is jailbroken, but some of my beta testers have reported that IAPs are not always used when their device is locked.

It seems you are using the class correctly.

+1
source

All Articles