简体   繁体   English

在iOS应用中对用户进行身份验证后继续流程的最佳实践是什么?

[英]What is the best practice to continue flow after user authentication in iOS app?

In my AppDelegate I check if there is already a user logged in. 在我的AppDelegate中,检查是否已经有用户登录。

if (userExists) {
        // Set my main view as the root view controller.
        UINavigationController *navController = [[UINavigationController alloc] init];
        MainViewController *mainVC = [[MainViewController alloc] init];
        [navController setRootViewController:mainVC animated:YES];
        [self.window setRootViewController:navController];
}

If there is no existing user I present a login screen. 如果没有现有用户,则显示登录屏幕。

Now, once the user has logged in I have to continue the flow, showing the MainViewController . 现在,一旦用户登录,我就必须继续该流程,显示MainViewController

I am currently doing the following, in a block that gets called when the user is successfully authenticated: 我当前正在执行以下操作,在成功验证用户身份时调用的块中:

UINavigationController *navController = [[UINavigationController alloc] init];
MainViewController *mainVC = [[MainViewController alloc] init];
[navController setRootViewController:wordViewController animated:YES];
UIApplication *application = [UIApplication sharedApplication];
[application.windows.firstObject setRootViewController:navController];

Is this a good way to go? 这是个好方法吗? Since this is a frequently repeated pattern throughout iOS apps, what is the most common, sensible way of doing this? 由于这是在iOS应用程序中经常重复出现的模式,因此最常见,最明智的方法是什么?

You could refactor the code that continues the flow when the user is authenticated. 您可以重构验证用户身份后继续执行流程的代码。 You can call it either immediately, or upon the successful login. 您可以立即调用,也可以在成功登录后调用它。 For example: 例如:

 - (void)showMainScreenForUser:(UserType *)user
{
        UINavigationController *navController = [[UINavigationController alloc] init];
        MainViewController *mainVC = [[MainViewController alloc] initWithUser:user];
        [navController setRootViewController:mainVC animated:YES];
        [self.window setRootViewController:navController];
}

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

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