IOS store more than one password in keychain

独自空忆成欢 提交于 2019-12-25 02:59:39

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/12324831/ios-store-more-than-one-password-in-keychain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!