Question

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

}
Was it helpful?

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;
    }

OTHER TIPS

Why don't you always start your app with a "Loading" view. The LoadingViewController checks if the user is logged in or not and then transitions to the proper viewcontroller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top