Search code examples
iosnavigationstoryboard

How to programmatically set which screen load first by story board


I am working on a app which have login screen. It records whether user logged in or not, in user defaults. I wanted to direct user to the login page if he has not logged in otherwise navigate to the apps main screen. How to do this programmatically using story board.

if(![[NSUserDefaults standardUserDefaults] boolForKey:@"loggedin"]) {

    //If logged in

} else {

   //if logged out

}

Solution

  • In your appdelegate write this code. And dont forget to give identifer name to your viewcontrollers

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        {
            self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    
            UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
    
            self.window.rootViewController = viewController;
            [self.window makeKeyAndVisible];
    
            return YES;
        }