简体   繁体   中英

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

我想检查用户是否登录,如果用户登录,则将他带到主屏幕,或显示欢迎屏幕。

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

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
}

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 :)

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()
        }

    } 

在 Swift 中,在你的 AppDelegate.swift 中,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM