I discovered the problem and by the way, I hate with passion the crappy documentation Apple writes about everything.
This is the problem. Despite the documentation suggesting that you should store passwords like plain strings like this using kSecValueData
:
NSString *myPassword = @"12345";
KeychainItemWrapper* keychain = [[KeychainItemWrapper alloc] initWithIdentifier:identifier accessGroup:nil];
[keychain setObject:myPassword forKey:(__bridge id)kSecValueData];
you should never do that. In fact kSecValueData
, as the name suggests and the documentation should say, expects a NSData
object. So, you must convert the NSString
to NSData before storing it, like this:
NSString *myPassword = @"12345";
KeychainItemWrapper* keychain = [[KeychainItemWrapper alloc] initWithIdentifier:identifier accessGroup:nil];
NSData *myPasswordData = [myPassword dataUsingEncoding:NSUTF8StringEncoding];
[keychain setObject:myPasswordData forKey:(__bridge id)kSecValueData];
when getting it back you must cast it to NSData
and convert it back to NSString
:
NSData *myPasswordData = (NSData *)[keychain objectForKey:(__bridge id)kSecValueData];
NSString *myPassword = [[NSString alloc] initWithData: myPasswordData
encoding:NSUTF8StringEncoding];