views:

584

answers:

1

I can't get the KeychainItemWrapper (Apple example) to work. I've added the KeychainItemWrapper files to my project and when running it on the phone, an exception is thrown by SecItemAdd, saying that one or more parameters were not valid (result code -50). The code triggering the SecItemAdd follows:

KeychainItemWrapper* wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"something" accessGroup:nil];
[wrapper setObject:@"this is my password" forKey:@"password"];
NSLog(@"Password: %@", [wrapper objectForKey:@"password"]);

What is wrong?

The code can be found at http://developer.apple.com/iphone/library/samplecode/GenericKeychain/index.html

+1  A: 

I ran into this same issue. You can't put arbitrary keys in the dictionary, you need to use well-defined keys that the SecItemAdd understands.

Try this:

KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"password" accessGroup:nil];
[wrapper setObject:@"this is my password" forKey:(id)kSecValueData];
NSLog(@"password: [%@]", [wrapper objectForKey:(id)kSecValueData]);
[wrapper release];
mlaster