1

I am trying to store and retrieve a string value to my keychain, I have been using the example found here however when I call mySetObject method my app is crashing.

This is what the code looks like that I am using to access the keychain class method mySetObject

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Test keychain Wrapper for future use through out connectionClasses
    KeychainWrapper *keychainWrapper = [[KeychainWrapper alloc] init];

    // set key
    [keychainWrapper mySetObject:@"myObj" forKey:@"entry1"]; // this is where my app falls over

    // read key

    NSString *myAwesomeID = [keychainWrapper myObjectForKey:@"myObj"];
    NSLog(@"%@", myAwesomeID);

//..

the error I am reciving in my output is as follows

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't add the Keychain Item.'
*** First throw call stack:

any help would be greatly appreciated.

HurkNburkS
  • 5,492
  • 19
  • 100
  • 183
  • 2
    You can't simply use keychain as a dictionary. It works only with predefined sets of keys. Check http://stackoverflow.com/a/5008417/653513 and an answer below that shows ARC usage: http://stackoverflow.com/a/14243924/653513 – Rok Jarc May 08 '13 at 23:53
  • 2
    this link helped me. http://b2cloud.com.au/how-to-guides/using-the-keychain-to-store-passwords-on-ios I didnt realise I was to use the speial list of functions (not sure if thats the right word for them... but for example this kSecAttrAccount – HurkNburkS May 09 '13 at 04:53

1 Answers1

0

This wrapper (KeychainItemWrapper) is similar to an NSDictionary, you set values for keys, however you don’t get to choose the keys. Please refer blog posting

So can't do:

// set key
[keychainWrapper mySetObject:@"myObj" forKey:@"entry1"]; // this is where my app falls over
Thiru
  • 1,380
  • 13
  • 21
  • thanks that helped me. also when using KeychainWrapper hopefully this helps someone, i noticed i need to 'bridge cast' the keys, because the compiler generates warnings, unless you do [keychain mySetObject:@"mypassword" forKey:(__bridge NSString *)kSecValueData]; – user1709076 Jun 07 '17 at 16:02