Sinuhe Huidobro
Sinuhe Huidobro

Reputation: 191

IOS store more than one password in keychain

in my app I can save an read fine 1 password store in the keychain using this code

// save password
[keychainItem setObject:textFieldPassword.text forKey:(__bridge id)(kSecValueData)];

//get pasword from keychain
NSString *_password = [keychainItem objectForKey:(__bridge id)(kSecValueData)];

My question is: how can I store more than 1 password at a time in the keychain?

Upvotes: 0

Views: 1166

Answers (2)

Sinuhe Huidobro
Sinuhe Huidobro

Reputation: 191

Thank you all for your answers.

Here the solution I used:

adding to my project the files KeychainItemWrapper.h/m

allocating 2 keychain items:

//aloc for user password
keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"passowrdKey1" accessGroup:nil];
//aloc for user password2
keychainItem2 = [[KeychainItemWrapper alloc] initWithIdentifier:@"passowrdKey1" accessGroup:nil];

then just use this to read/write

//WRITE    
// save password
[keychainItem setObject:@"password1" forKey:(__bridge id)(kSecValueData)];
// save password2
[keychainItem2 setObject:@"password2" forKey:(__bridge id)(kSecValueData)];

//READ        
//get pasword from keychain
NSString *_pass = [keychainItem objectForKey:(__bridge id)(kSecValueData)];

//get pasword from keychain
NSString *_pass2 = [keychainItem2 objectForKey:(__bridge id)(kSecValueData)];

Upvotes: 0

Daddy
Daddy

Reputation: 9035

Keychain programming is hard. I use a wrapper class called SFHFKeychainUtils. It has very simple class methods for storing and retrieving passwords.

Check it out: https://github.com/ldandersen/scifihifi-iphone/tree/master/security

You store items with keys you make up. So you could have @"WiFiPasswordKey", @"LoginPasswordKey", etc.

Upvotes: 2

Related Questions