简体   繁体   English

如何使用iOS钥匙串存储多个用户名和密码?

[英]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. 我目前正在使用Apple的KeychainWrapper来存储一个用户名和密码,但我想添加能够登录到不同帐户并在我的应用程序之间切换的功能。

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? 是将不同钥匙串的标识符存储在plist中,然后每次我想更改帐户时都重新初始化钥匙串吗? or is there a better approach to this? 还是有更好的方法呢? maybe a NSMutableArray of keychains? 也许是NSMutableArray的钥匙串?

Thanks, Nick. 谢谢,尼克。

I suggest you to check the open source SFHFKeychainUtils class. 我建议您检查开放源代码SFHFKeychainUtils类。

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 . 它在GitHub中可用 You just need to drag and drop the classes and import the Security framework to your project. 您只需要拖放类并将Security框架导入到您的项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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