Search code examples
iosswiftviewcontroller

How can I change the initial ViewController in LaunchScreen with Swift?


I want to check whether the user is logged in or not, if the user is logged in, then bring him to main screen, or show the welcome screen.


Solution

  • You can't do in Launch Screen, but you can achieve same in AppDelegate's method didFinishLauchingWithOption, there you can check if user logged in or not and set the root view controller and don't set initialViewController in storyboard. It should be something like this

        NSString *identifier = isLoggedIn ? @"IdentifierForVC1" : @"IdentifierForVC2";
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier: identifier];
        self.window.rootViewController = vc;
    

    Code is not tested in an editor may have some Swift code should be like this

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier(identifier) as! UIViewController
    self.window?.rootViewController = vc