简体   繁体   中英

How can I store multiple usernames and passwords with the iOS keychain?

I'm currently using Apple's KeychainWrapper to store a single username and password, but i'd like to add the functionality of being able to login to different accounts, and switching between them in my app.

Is it a matter of storing identifiers for different keychains in a plist, and then re-initializing the keychain each time I want to change account? or is there a better approach to this? maybe a NSMutableArray of keychains?

Thanks, Nick.

I suggest you to check the open source SFHFKeychainUtils class.

It is extremely simple, you just need to call one method to add (or update):

+ (BOOL) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error;

...one method to get the stored password:

+ (NSString *) getPasswordForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error;

...and one method to delete:

+ (BOOL) deleteItemForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error;

It is available in GitHub . You just need to drag and drop the classes and import the Security framework to your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM