简体   繁体   English

使用目标c保存用户名和密码

[英]To save username & password using objective c

我有登录视图,登录后会显示一些列表...所以我想保存用户名和密码,这样如果使用正确的ID登录,则登录视图不会出现一次,直接输入密码会显示该列表

As others have suggested, you can use NSUserDefaults . 正如其他人所建议的那样,您可以使用NSUserDefaults

If you are storing the password, then I would suggest you also add a level of security to this. 如果要存储密码,那么我建议您还为此添加一个安全级别。 You could use Secure NSUserDefaults (I've not used this personally, but seen a few people reporting it being useful). 您可以使用Secure NSUserDefaults(我个人没有使用过,但是看到一些人报告它很有用)。 You can find that here 你可以在这里找到

Or you can use the KeyChain API 或者您可以使用KeyChain API

If you do not care about encryption, you could save these very simply with 如果您不关心加密,则可以使用

[[NSUserDefaults standardUserDefaults] setValue:@"username" forKey:@"username"]; 
[[NSUserDefaults standardUserDefaults] setValue:@"secret" forKey:@"password"]; 

and check those during program startup with 并在程序启动过程中使用

if (![[NSUserDefaults standardUserDefaults] valueForKey:@"username"]) {
   // show the login screen
}

您可以使用NSUserDefaults 有关NSUserDefaults的Apple文档

You can save this in the NSUserDefaults like this : 您可以像这样将其保存在NSUserDefaults中:

NSUserDefaults *userDefaults = [NSUserDefaults standardDefaults];
[userDefaults setBool@"YES" forlkey@"userDidLogin"];

And you test like this : 你这样测试:

NSUserDefaults *userDefaults = [NSUserDefaults standardDefaults];

if([userDefaults boolForKey:@"userDidLogin"])
{
.... // Go to the list View
}

This is just an indication, if you would like to put it to work , you should show your code how you implement the implémentation of login interface 这只是一个指示,如果您想使用它,则应该向您的代码展示如何实现登录界面的实现

NSUserDefaults is just a handy way designed for customize app's preferences. NSUserDefaults只是一种用于自定义应用程序首选项的便捷方法。 In normal situation, an app will stay in it's own sandbox and therefore other apps won't be able to access it. 在正常情况下,一个应用程序将保留在其自己的沙箱中,因此其他应用程序将无法访问它。

However it is not the best choice to do so. 但是,这不是最佳选择。 It's not good practice. 这不是一个好习惯。 What you really need is keychain and see here for apple's sample code for it, it's much safer in terms of security. 您真正需要的是钥匙串,并在此处查看Apple的示例代码,它在安全性方面更加安全。

there are multiple ways to save user name and password.. You can save in UserDefalt as above answer. 保存用户名和密码的方法有多种。您可以按上述答案保存在UserDefalt中。 also you can save in plist. 您也可以保存在plist中。

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

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