Nycen Nycen - 8 months ago 348
iOS Question

Storing email in keychain impossible (KeychainItemWrapper)

I'm using the ARCified version of KeychainItemWrapper available at github, and I can't get it to store both email and password.

KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"myApp" accessGroup:@""];
[keychainItem setObject:[ dataUsingEncoding:NSUTF8StringEncoding] forKey:(__bridge id)kSecAttrAccount];
[keychainItem setObject:self.password forKey:(__bridge id)kSecValueData];

I works perfectly as long as I store an email... that don't have an at sign (@) it. Otherwise, I get the error

*** Assertion failure in -[KeychainItemWrapper writeToKeychain]
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't add the Keychain Item.'

Which comes from those lines

result = SecItemAdd((__bridge CFDictionaryRef)[self dictionaryToSecItemFormat:keychainItemData], NULL);
NSAssert( result == noErr, @"Couldn't add the Keychain Item." );

Do you have any idea about what can be going wrong here ?



Well, I just found the answer following the advice on that page iOS KeychainItemWrapper not updating, which is to add

[keychainWrapper setObject:@"Myappstring" forKey: (id)kSecAttrService];