skip to Main Content

I know, there are a lot of similar threads but I’m still struggling to get this problem solved.

I want to instantiate a certain viewcontroller in AppDelegate (only if a user is not logged in). But even without an authentication check my code in app delegate to instantiate my login controller seems to be ignored:

class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        self.window = UIWindow(frame: UIScreen.main.bounds)
        let loginController = UIStoryboard(name: "Main", bundle: .main).instantiateViewController(withIdentifier: "loginController") as! LoginController
        self.window!.rootViewController = loginController
        self.window!.makeKeyAndVisible()
        
        return true
    }
}

No matter which view controller I instantiate in app delegate, the initial view controller set in storyboard appears when launching my app. (Note: if no view controller is set as initial in storyboard, the app is launching with a black screen).
What’s my mistake?

Thanks a lot in advance
Alex

2

Answers


  1. You can try this code in didFinishLaunchingWithOptions method —

    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let initialVC: ViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
         let navigationController = UINavigationController(rootViewController: initialVC)
        navigationController.isNavigationBarHidden = true
        self.window?.rootViewController = navigationController self.window?.makeKeyAndVisible()
    
    Login or Signup to reply.
  2. In a modern iOS app, the app delegate has no window. You can create one but it will be replaced. The visible window belongs to the scene delegate. You need to create the interface using scene delegate code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search