0

I want to use keychain in order to store username, password and access token. I added the keychainItem.h and keyChainItem.m implemented here. And this is what I did:

1- I created a property keychain in myViewController.h then in the viewDidLoad I instantiate it like this:

self.keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"Login" accessGroup:nil];

When I got the userName, password and the access token this is what I did:

2- To store the userName

[self.keychain setObject:userName forKey:(__bridge id)kSecAttrAccount]; 

When I test it, it works.

Now I want to add the password and the access token.

For the access token I tried

[self.keychain setObject:accessToken forKey:(__bridge id)kSecAttrAccessible];

When I run it the application crash:

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

I tried to create another keychain

self.keychainToken = [[KeychainItemWrapper alloc] initWithIdentifier:@“Token” accessGroup:nil];

then I set the value:

[self.keychainToken setObject:accessToken forKey:(__bridge id)kSecValueData]; 

But I got the same error.

What is wrong with what I did? How can I store the user, password and the accessToken using keychain?

Ne AS
  • 1,490
  • 3
  • 26
  • 58

1 Answers1

0

You can use it like this:

self.keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"Login" accessGroup:nil];

[self.keychain setObject:userName forKey:(id)kSecAttrAccount];
[self.keychain setObject:password forKey:(__bridge id)(kSecValueData)];
[self.keychain setObject:accessToken forKey:(id)kSecAttrTokenID];
[self.keychain setObject:@"LoginService" forKey: (id)kSecAttrService];
Poles
  • 3,585
  • 9
  • 43
  • 91
  • And what about the password? – Ne AS Mar 22 '17 at 11:12
  • When I use kSecValueData for the accessToken it works you're right. But how can I store the password? – Ne AS Mar 22 '17 at 11:13
  • Sorry but the application crash with the same message – Ne AS Mar 22 '17 at 11:28
  • My bad. `kSecValueData` only store string value. I have updated the answer. Try it and let me know. – Poles Mar 22 '17 at 11:34
  • It woooorks thank you! Can you tell me please is kSecAttrService required or optional? What is it role? And if I want to store an NSDictionnary is that possible with something else than kSecValueData? Thaaaank again and sorry for all those questions – Ne AS Mar 22 '17 at 11:41
  • `NSDictionnary` is not possible. All keys are `CFStringRef` type in case of `KeychainItemWrapper`. There number of keys available. Just click on any of the keys and it will take you to `SecItem.h` of `Security` framework where all others keys are defined. And remove `kSecAttrService` and check if woks or not. – Poles Mar 22 '17 at 12:22
  • Thank you. It works without kSecAttrService. For the NSDictionnary, in your opinion, is it a good solution to use something like the answers in this question? http://stackoverflow.com/questions/9948698/store-nsdictionary-in-keychain . Or ideal is to use the keychain only for string because this is the goal of it (store password..)? – Ne AS Mar 22 '17 at 12:30
  • 1
    If you want to store only password and username then my code does the job but if you want to store lots of info then use according to the mentioned link. – Poles Mar 22 '17 at 12:50