xcode iOS:登录后如何对用户进行身份验证(用于自动登录)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10295504/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:04:22  来源:igfitidea点击:

iOS: How to authenticate a user after login (for auto login)?

iphoneiosxcodeautologin

提问by filou

I'd like to use an auto-login function. So when the user opens the app, he gets delegated to a "login screen". When he logged in successfully he should be directed to his account. I call this the "account screen". Now when the user restarts the app, he should directly get directed to his account, without seeing the "login screen".

我想使用自动登录功能。因此,当用户打开应用程序时,他会被委派到“登录屏幕”。当他成功登录时,他应该被定向到他的帐户。我称之为“帐户屏幕”。现在,当用户重新启动应用程序时,他应该直接进入他的帐户,而不会看到“登录屏幕”。

The login function already works fine in my project (username and password are saved in UserDefault), but every time I close the app, I have to login again. So my question is: How do auto login the user? Or better said: How do I check if the data (saved in UserDefault) is the same as in the database (MYSQL)?

登录功能在我的项目中已经可以正常工作(用户名和密码保存在UserDefault中),但每次关闭应用程序时,我都必须再次登录。所以我的问题是:如何自动登录用户?或者更好地说:如何检查数据(保存在 UserDefault 中)是否与数据库(MYSQL)中的相同?

采纳答案by filou

To answer the question: if you want to auto-login with keychain data, use the free framework "SFHFKeychainUtils". It saves username, password and servicename in keychain. if you want to retrieve it, just save the username in NSUserDefaults and you can get the password with ease.

回答这个问题:如果您想使用钥匙串数据自动登录,请使用免费框架“SFHFKeychainUtils”。它将用户名、密码和服务名称保存在钥匙串中。如果您想找回它,只需将用户名保存在 NSUserDefaults 中,您就可以轻松获取密码。

Here we go:

开始了:

SiFi HiFi Framework:https://github.com/ldandersen/scifihifi-iphone/tree/master/security

SiFi HiFi 框架:https : //github.com/ldandersen/scifihifi-iphone/tree/master/security

SiFi Hifi Framework (ARC compatible):https://stackoverflow.com/a/10348964/1011125

SiFi Hifi 框架(ARC 兼容):https : //stackoverflow.com/a/10348964/1011125

How to use SFHFKeychainUtils:http://gorgando.com/blog/technology/iphone_development/simple-iphone-tutorial-password-management-using-the-keychain-by-using-sfhfkeychainutils

如何使用 SFHFKeychainUtils:http ://gorgando.com/blog/technology/iphone_development/simple-iphone-tutorial-password-management-using-the-keychain-by-using-sfhfkeychainutils

回答by Krishnabhadra

  1. For the first time when the user login, you save the user credentials in iPhone's keychain.
  2. When the app is opened again, you check whether user credentials are present in keychain and if yes, you code should call the login logic and do auto login and go to screen after login screen. If no, then you should show login screen. You can do this logic in AppDelegates applicationDidFinishLaunching.
  3. Whenever user clicks the logout button, remove user credentials from keychain first, and go back to login controller.
  1. 用户第一次登录时,您将用户凭据保存在 iPhone 的钥匙串中。
  2. 当应用程序再次打开时,您检查钥匙串中是否存在用户凭据,如果是,您的代码应该调用登录逻辑并执行自动登录并在登录屏幕后转到屏幕。如果没有,那么您应该显示登录屏幕。您可以在 AppDelegates applicationDidFinishLaunching 中执行此逻辑。
  3. 每当用户单击注销按钮时,首先从钥匙串中删除用户凭据,然后返回登录控制器。

Simply you add login credentials to keychain when user logs in and only remove it once user clicks the logout button. If user quits the app without logout then the credentials will still be in keychain and you can retrieve them when user returns to the app.

只需在用户登录时将登录凭据添加到钥匙串,并且只有在用户单击注销按钮后才将其删除。如果用户在没有注销的情况下退出应用程序,那么凭据仍将在钥匙串中,您可以在用户返回应用程序时检索它们。

EDIT:I think I must add one more thing..If your login logic takes time (like you login using web request or something), put the login logic code in your Login ViewController rather than ApplicationDelegate, and use any Activity Indicator during auto login process.

编辑:我想我必须再添加一件事。过程。

EDIT: I edited the entire answer, replaced NSUserDefault with Keychain. Thisthread explains why.

编辑:我编辑了整个答案,用钥匙串替换了 NSUserDefault。这个线程解释了原因。

回答by Roshit

While saving Username and Password, it is highly advised to save in Keychain rather than the NSUserDefaults. Refer this postfor a better understanding.

在保存用户名和密码时,强烈建议保存在 Keychain 而不是 NSUserDefaults。参考这篇文章以获得更好的理解。

回答by Krish

I used a combination of NSUserDefaults and SSKeychain. I used NSUserDefaults to store the username nad SSKeychain to store the password.

我使用了 NSUserDefaults 和 SSKeychain 的组合。我使用 NSUserDefaults 来存储用户名和 SSKeychain 来存储密码。

This is the code I used to save the credentials

这是我用来保存凭据的代码

NSString *user = self.username.text;
NSString *password = self.pass.text;
[SSKeychain setPassword:password forService:@"achat" account:user];
NSUserDefaults *dUser = [NSUserDefaults standardUserDefaults];
[dUser setObject:user forKey:@"user"];
[dUser synchronize];

This is the code I used to retrieve the credentials

这是我用来检索凭据的代码

NSUserDefaults *eUser = [NSUserDefaults standardUserDefaults];
NSString *savedUser = [eUser objectForKey:@"user"];

    if (!savedUser) {
        UIAlertView *uhoh = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Please enter your username and password." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
        [uhoh show];
    }

    else {
            NSString *savedPass = [SSKeychain passwordForService:@"achat" account:savedUser];
            self.username.text = savedUser;
            self.pass.text = savedPass;
         }