Nick Triantafillou
Nick Triantafillou

Reputation: 585

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.

Upvotes: 0

Views: 1744

Answers (1)

Natan R.
Natan R.

Reputation: 5181

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.

Upvotes: 3

Related Questions