iOS:如何在应用程序中存储用户名/密码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6972092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: How to store username/password within an app?
提问by Kovu
I have a login-screen in my iOS app.
The username and password will be saved in the NSUserDefaults
and be loaded into the login-screen again when you enter the app again (of course, NSUserDefaults
are permanent).
我的 iOS 应用中有一个登录屏幕。用户名和密码将被保存在,NSUserDefaults
当您再次进入应用程序时再次加载到登录屏幕(当然,NSUserDefaults
是永久的)。
Now, the user have the possibility to disable the username/password saving feature.
现在,用户可以禁用用户名/密码保存功能。
So the NSUserDefaults
will be cleared then.
所以NSUserDefaults
那时将被清除。
But In my app I need this username/password for database queries for the user.
So: Where to store the data except NSUserDefaults
?
(This place can / should be deleted when the user quit the app or logout).
但是在我的应用程序中,我需要这个用户名/密码来为用户查询数据库。那么:除了NSUserDefaults
?在哪里存储数据?(当用户退出应用程序或注销时,这个地方可以/应该被删除)。
回答by Filip Radelic
You should always use Keychain to store usernames and passwords, and since it's stored securely and only accessible to your app, there is no need to delete it when app quits (if that was your concern).
您应该始终使用钥匙串来存储用户名和密码,并且由于它被安全地存储并且只能由您的应用程序访问,因此在应用程序退出时无需将其删除(如果您担心的话)。
Apple provides sample codethat stores, reads and deletes keychain items and here is how to use the keychain wrapper class from that sample which greatly simplifies using Keychain.
Apple 提供了存储、读取和删除钥匙串项目的示例代码,这里是如何使用该示例中的钥匙串包装类,这极大地简化了钥匙串的使用。
Include Security.framework (in Xcode 3 right-click on frameworks folder and add existing framework. In Xcode 4 select your project, then select target, go to Build Phases tab and click + under Link Binary With Files)and KeychainItemWrapper .h & .m files into your project, #import the .h file wherever you need to use keychain and then create an instance of this class:
包括 Security.framework (在 Xcode 3 中右键单击 frameworks 文件夹并添加现有框架。在 Xcode 4 中选择您的项目,然后选择目标,转到 Build Phases 选项卡并单击 Link Binary With Files 下的 +)和 KeychainItemWrapper .h & 。 m 文件到您的项目中,#import .h 文件在您需要使用钥匙串的任何地方,然后创建此类的实例:
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"YourAppLogin" accessGroup:nil];
(YourAppLogincan be anything you chose to call your Keychain item and you can have multiple items if required)
(YourAppLogin可以是您选择调用钥匙串项目的任何内容,如果需要,您可以拥有多个项目)
Then you can set the username and password using:
然后您可以使用以下方法设置用户名和密码:
[keychainItem setObject:@"password you are saving" forKey:kSecValueData];
[keychainItem setObject:@"username you are saving" forKey:kSecAttrAccount];
Get them using:
让他们使用:
NSString *password = [keychainItem objectForKey:kSecValueData];
NSString *username = [keychainItem objectForKey:kSecAttrAccount];
Or delete them using:
或使用以下方法删除它们:
[keychainItem resetKeychainItem];
回答by Manuel Pintaldi
If you need an ARC version of the wrapper here is the link https://gist.github.com/1170641Thanks to
如果你需要一个 ARC 版本的包装器,这里是链接https://gist.github.com/1170641感谢
回答by sust86
A very easy solution via Keychains.
通过Keychains 的一个非常简单的解决方案。
It's a simple wrapper for the system Keychain. Just add the SSKeychain.h
, SSKeychain.m
, SSKeychainQuery.h
and SSKeychainQuery.m
files to your project and add the Security.framework to your target.
它是系统钥匙串的简单包装。就在增加SSKeychain.h
,SSKeychain.m
,SSKeychainQuery.h
和SSKeychainQuery.m
文件到您的项目,并添加Security.framework你的目标。
To save a password:
要保存密码:
[SSKeychain setPassword:@"AnyPassword" forService:@"AnyService" account:@"AnyUser"]
To retrieve a password:
找回密码:
NSString *password = [SSKeychain passwordForService:@"AnyService" account:@"AnyUser"];
Where setPassword
is what value you want saved and forService
is what variable you want it saved under and account is for what user/object the password and any other info is for.
setPassword
您要保存的值在哪里以及forService
您希望将其保存在什么变量下,帐户是用于密码和任何其他信息的用户/对象。
回答by Phil
回答by D_RBD
I decided to answer how to use keychain in iOS 8 using Obj-C and ARC.
我决定回答如何使用 Obj-C 和 ARC 在 iOS 8 中使用钥匙串。
1)I used the keychainItemWrapper (ARCifief version) from GIST: https://gist.github.com/dhoerl/1170641/download- Add (+copy) the KeychainItemWrapper.h and .m to your project
1)我使用了 GIST 的 keychainItemWrapper(ARCifief 版本):https://gist.github.com/dhoerl/1170641/download - 将 KeychainItemWrapper.h 和 .m 添加(+copy)到您的项目中
2) Add the Security framework to your project (check in project > Build phases > Link binary with Libraries)
2) 将安全框架添加到您的项目中(检查项目 > 构建阶段 > 将二进制文件与库链接)
3) Add the security library (#import ) and KeychainItemWrapper (#import "KeychainItemWrapper.h") to the .h and .m file where you want to use keychain.
3) 将安全库 (#import ) 和 KeychainItemWrapper (#import "KeychainItemWrapper.h") 添加到要使用钥匙串的 .h 和 .m 文件中。
4) To save data to keychain:
4) 将数据保存到钥匙串:
NSString *emailAddress = self.txtEmail.text;
NSString *password = self.txtPasword.text;
//because keychain saves password as NSData object
NSData *pwdData = [password dataUsingEncoding:NSUTF8StringEncoding];
//Save item
self.keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"YourAppLogin" accessGroup:nil];
[self.keychainItem setObject:emailAddress forKey:(__bridge id)(kSecAttrAccount)];
[self.keychainItem setObject:pwdData forKey:(__bridge id)(kSecValueData)];
5) Read data (probably login screen on loading > viewDidLoad):
5)读取数据(可能是加载时的登录屏幕> viewDidLoad):
self.keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"YourAppLogin" accessGroup:nil];
self.txtEmail.text = [self.keychainItem objectForKey:(__bridge id)(kSecAttrAccount)];
//because label uses NSString and password is NSData object, conversion necessary
NSData *pwdData = [self.keychainItem objectForKey:(__bridge id)(kSecValueData)];
NSString *password = [[NSString alloc] initWithData:pwdData encoding:NSUTF8StringEncoding];
self.txtPassword.text = password;
Enjoy!
享受!
回答by Robby891
If you are having an issue retrieving the password using the keychain wrapper, use this code:
如果您在使用钥匙串包装器检索密码时遇到问题,请使用以下代码:
NSData *pass =[keychain objectForKey:(__bridge id)(kSecValueData)];
NSString *passworddecoded = [[NSString alloc] initWithData:pass
encoding:NSUTF8StringEncoding];
回答by BSevo
checkout this sample codei tried first the apple's wrapper from the sample code but this is much simpler for me
回答by Vara
try this one:
试试这个:
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"YourAppLogin" accessGroup:nil];
[keychainItem setObject:@"password you are saving" forKey:kSecValueData];
[keychainItem setObject:@"username you are saving" forKey:kSecAttrAccount];
may it will help.
可能会有所帮助。
回答by Carl
I looked at using KeychainItemWrapper (the ARC version) but I didn't find its Objective C wrapper as wholesome as desired.
我查看了使用 KeychainItemWrapper(ARC 版本),但我没有发现它的 Objective C 包装器如我所愿。
I used this solutionby Kishikawa Katsumi, which meant I wrote less code and didn't have to use casts to store NSString values.
我使用了Kishikawa Katsumi 的这个解决方案,这意味着我编写的代码更少,而且不必使用强制转换来存储 NSString 值。
Two examples of storing:
两个存储示例:
[UICKeyChainStore setString:@"kishikawakatsumi" forKey:@"username"];
[UICKeyChainStore setString:@"P455_w0rd$G$Z$" forKey:@"password"];
Two examples of retrieving
检索的两个例子
UICKeyChainStore *store = [UICKeyChainStore keyChainStore];
// or
UICKeyChainStore *store = [UICKeyChainStore keyChainStoreWithService:@"YOUR_SERVICE"];
NSString *username = [store stringForKey:@"username"];
NSString *password = [store stringForKey:@"password"];
回答by ReaLity
There is a small bug in the above code (by the way Dave it was very helpful your post thank you)
上面的代码中有一个小错误(顺便说一句,戴夫对你的帖子很有帮助,谢谢)
In the part where we save the credentials it also needs the following code in order to work properly.
在我们保存凭据的部分,它还需要以下代码才能正常工作。
[self.keychainItem setObject:@"myCredentials" forKey:(__bridge id)(kSecAttrService)];
most probably is because the second time we try to (re-)sign in with the same credentials it finds them already assigned in the keychain items and the app crashes. with the above code it works like a charm.
很可能是因为我们第二次尝试使用相同的凭据(重新)登录时,它发现它们已分配到钥匙串项目中,并且应用程序崩溃了。使用上面的代码,它就像一个魅力。