0

In my AppDelegate I check if there is already a user logged in.

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.

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?

Aaron
  • 7,055
  • 2
  • 38
  • 53
ayberkt
  • 105
  • 1
  • 5
  • possible duplicate of [Best practices for Storyboard login screen, handling clearing of data upon logout](http://stackoverflow.com/questions/19962276/best-practices-for-storyboard-login-screen-handling-clearing-of-data-upon-logou) – vokilam Mar 03 '14 at 19:28

1 Answers1

0

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];
}
TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76