David
David

Reputation: 1680

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.

Upvotes: 3

Views: 9556

Answers (5)

Rashid Latif
Rashid Latif

Reputation: 2901

Write this code in AppDelegate Swift 4.0

In didFinishLaunchingWithOptions pass your viewController to self.launch(rootController: ViewController())

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    let frame = UIScreen.main.bounds
    self.window = UIWindow(frame: frame)
    self.window!.rootViewController = ViewController()
    self.window!.makeKeyAndVisible()
    return true
 }

Write this code in AppDelegate Swift 5.0 Xcode 11.0

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        guard let _ = (scene as? UIWindowScene) else { return }

        if let windowScene = scene as? UIWindowScene{

            let window = UIWindow(windowScene: windowScene)
            let rootViewController = UIStoryboard(name: "Auth", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! UINavigationController

            window.rootViewController = rootViewController
            self.window = window
            window.makeKeyAndVisible()
        }

    } 

Upvotes: 1

Darkngs
Darkngs

Reputation: 6459

Swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
   self.window = UIWindow(frame: UIScreen.main.bounds)
   let storyboard = UIStoryboard(name: "Main", bundle: nil)
   let viewController = storyboard.instantiateViewController(withIdentifier: "Identifier")
   let navigationController = UINavigationController(rootViewController: viewController)
   self.window?.rootViewController = navigationController
   self.window?.makeKeyAndVisible()

   return true
}

Upvotes: 2

theNoobDev10
theNoobDev10

Reputation: 425

You can't do it in Launch Screen but can do in AppDelegate

for Swift 4

 if userLoggedIn == True {
        //Do something 
        } else {
        //Do something
        }
        UserDefaults.standard.set(value:Bool  ,forKey:"loggedIn")  //This will save the bool value to your UserDefaults
      }


Now Go to your AppDelegate


     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

           self.window = UIWindow(frame: UIScreen.main.bounds)
            let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)


     if (UserDefaults.standard.bool(for key: "loggedIn")) == true {
         let welcomeVC = mainStoryboard.instantiateViewController(withIdentifier: "welcomeVC") as! WelcomeVC
            self.window?.rootViewController = newRootVC

            self.window?.makeKeyAndVisible()
        } else {
         let loginVC = mainStoryboard.instantiateViewController(withIdentifier: "loginVC") as! LoginVC
            self.window?.rootViewController = newRootVC

            self.window?.makeKeyAndVisible()
        }
return true
    }

Happy Coding Hope this helps :)

Upvotes: 1

Adnan Aftab
Adnan Aftab

Reputation: 14477

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

Upvotes: 7

Evo Sae
Evo Sae

Reputation: 73

In Swift, in your AppDelegate.swift,

self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("{INSERT_STORYBOARD_ID_HERE}") as? UIViewController

Upvotes: 0

Related Questions