Author Topic: How can I display a login screen when my app first launches?  (Read 6419 times)


  • Administrator
  • *****
  • Posts: 719
    • View Profile
Follow these steps to display a login screen when your app first launches:

1. Create a login scene and associated view controller.

2. Select your login scene in the project's storyboard (if you have a separate navigation controller that launches the login scene, select that instead). Go to the Identity Inspector and under the Identity section, set the Storyboard ID to LoginViewController. Afterwards, select the Use Storyboard ID check box.

3. Go to your AppDelegate.h file and add the following property:

Code: [Select]
@property (strong, nonatomic) UIViewController *rootViewController;
4. Go to your AppDelegate.m file and add the following code to the application:didFinishLaunchingWithOptions: method (this code assumes you have an isLoggedOut flag to indicate if the user is logged out):

Code: [Select]
        if (isLoggedOut) {
            // Get a reference to the storyboard
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

            // Get a reference to the login view controller
            UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
            // Save a reference to the current root view controller
            self.rootViewController = self.window.rootViewController;

            // Set the login view controller as the root view controller
            [self.window setRootViewController:vc];

Note: Make sure you change the name of the storyboard in the second line of code if your storyboard is not named MainStoryboard!

5. In your log in view controller's .m file, add the following import statement to the top of the file:

Code: [Select]
#import "AppDelegate.h"
6. Add the following code that replaces the original root view controller and transitions to it:

Code: [Select]
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    [UIView transitionFromView:appDelegate.window.rootViewController.view
                    completion:^(BOOL finished){
                         appDelegate.window.rootViewController = nil;
                        appDelegate.window.rootViewController = appDelegate.rootViewController;

    [appDelegate.window setRootViewController:appDelegate.rootViewController];

7. There seems to be a bit of a bug when transitioning from one scene to the next that causes the navigation bar to "flutter". To avoid this problem, add the following code to the viewWillAppear: method of the view controller that gets displayed after you transition to the root view controller (NOT in the root view controller, but in the view controller that gets displayed by the root view controller):

Code: [Select]
- (void)viewWillAppear:(BOOL)animated
    [super viewWillAppear:animated];
    // Need this or the transition from login screen to home screen
    // flashes black near the bottom of the navigation bar
    [self.navigationController.navigationBar.layer removeAllAnimations];

That's it...all the best!
« Last Edit: May 23, 2014, 02:47:20 PM by kjmcneish »