Storing keys in KeyChain with KeyChainItemWrapper

24,033

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];

Share:
24,033
Oksana
Author by

Oksana

Ruby/nodejs/js/iOs developer/unity3d

Updated on July 16, 2022

Comments

  • Oksana
    Oksana almost 2 years

    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?

  • Damien MATHIEU
    Damien MATHIEU almost 12 years
    When using the ARC version of keychainItemWrapper, you need to do it the following way : [_authenticationTokenKeychain setValue:authenticationToken forKey:(__bridge NSString*)kSecValueData];
  • Interfector
    Interfector over 11 years
    @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?
  • fdezjose
    fdezjose over 10 years
    @Interfector instead of using setValue, use setObject