Search code examples
iosswiftstartupappdelegateentry-point

Set App Entry point programmatically in AppDelegate


In my appdelegate I want to check if globUs.hasName. If it does I want the Entry Point of the app to be my main storyboard. If it does not I want the Entry Point of the app to be my newUser storyboard. How do I set the entry point of the app? If I can't, what is the most effective way to implement this functionality?


Solution

  • Consider not having an entry point. Then, in appDelegate, test for your variable and choose the appropriate storyboard accordingly. Then show the view controller from that storyboard.

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        if globUs.hasName {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "FirstMainVC")
            self.window = UIWindow(frame: UIScreen.main.bounds)
            self.window?.rootViewController = new
            self.window?.makeKeyAndVisible()
        }
        else {
            let storyboard = UIStoryboard(name: "NewUser", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "FirstNewUserVC")
            self.window = UIWindow(frame: UIScreen.main.bounds)
            self.window?.rootViewController = welcomeVC
            self.window?.makeKeyAndVisible()
        }
    
        return true
    }