Going directly to Main View controller for already logged in user

2.9k Views Asked by At

I have a loginViewController as my rootviewcontroller followed by the main screen and then other screens. My views follow push and pop approach. What I want is, if a user is already logged in my view should start from main screen else start from login screen, and if I logout from main screen it should go back to login screen and the push and pop structure should be maintained. I can achieve this using the modal transition, but I need to use push and pop approach, is this possible? Currently I have checked a already logged in condition in my appdelegate to set the rootview controller but it fails if I attempt to log out as its not present in my navigation controller stack.

if !alreadyLoggedin
{
    let mainListVC = storyBoard.instantiateViewControllerWithIdentifier(“MainListViewController”)
    self.window!.rootViewController = mainListVC
}
else
{
    let loginVC = storyBoard.instantiateViewControllerWithIdentifier("ViewController")
    self.window!.rootViewController = loginVC
}
2

There are 2 best solutions below

4
On BEST ANSWER

It's basic use case, Please follow my steps.

  1. In your story create navigation view controller with view controller.
  2. Select navigation view controller as your initial view controller in storyboard.
  3. Create three views controllers- InitialViewController, LoginViewController and MainViewController and set storyboard id for all view controllers.

Created sample project for you. Download link (https://www.dropbox.com/s/zk8x7ptg5mzmotk/test.zip?dl=0)

2
On

You need to use. AppDelegate

Objective C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"LoginStatus"]) {
        UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"loginController"];
    }else{
        UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"RootView"];

    }

}

SWIFT - i hope its correct

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

      //Using UserDefaults check already loggedin user or not
        if (!someStatus) {

            let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
            self.presentViewController(vc, animated: true, completion: nil)

        }else{


            let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("loginViewController") as! UIViewController
            self.presentViewController(vc, animated: true, completion: nil)

        }

    }