Storing keys in KeyChain with KeyChainItemWrapper
Asked Answered
S

2

15

I'm using KeyChainItemWrapper class, provided by Apple's Sample Code to save the authentication token to the keychain.

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

But when I'm trying to set the value to keychain, an odd exception is raised

[_authenticationTokenKeychain setObject:authenticationToken forKey: @"auth_token"];

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't add the Keychain Item.'

The keychain doesn't exist yet (at the moment of this call) What can cause this exception?

Soakage answered 19/8, 2011 at 6:44 Comment(1)
Make sure you added the keychain access plist file. Take a look at #5860115Lintwhite
G
33

You need to use standard keys, so here your @"auth_token" is incorrect.

The keys that can be used for this purpose and the possible values for each key are listed in the “Keychain Services Constants” section.

source, with list of valid constants: Keychain Services Reference

For instance, you can use:

[_authenticationTokenKeychain setObject:authenticationToken forKey: (__bridge NSString *)kSecValueData];

Greenfinch answered 23/1, 2012 at 17:8 Comment(3)
When using the ARC version of keychainItemWrapper, you need to do it the following way : [_authenticationTokenKeychain setValue:authenticationToken forKey:(__bridge NSString*)kSecValueData];Zales
@DamienMATHIEU I'm using the ARC version but I'm still having issues. I get this error: '[<KeychainItemWrapper 0x89c5900> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key v_Data.' Any ideas what could be wrong?Yokoyokohama
@Yokoyokohama instead of using setValue, use setObjectAnatolian
G
0
[_authenticationTokenKeychain setObject:authenticationToken forKey: @"auth_token"];  

For code snippet above, key param is only can use the keys provided from sdk. You can find all in SecItem.h

Garmaise answered 25/5, 2015 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.