1

I need to implement the following check in my AppDelegate

var window: UIWindow?

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

    // If logged in, jump to tabs
    let isUserLoggedIn = UserDefaults.standard.bool(forKey: "isUserLoggedIn")
    print(isUserLoggedIn)

    if isUserLoggedIn {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let tabBarController = storyboard.instantiateViewController(withIdentifier: "tabs") as! TabBarController
        self.window?.rootViewController = tabBarController
        self.window?.makeKeyAndVisible()
    }
    return true
}

The problem is I'm using Xcode 11 which has the new AppDelegate / SceneDelegate setup. Because of this, the UIWindow is nil, so the new rootViewController is not set. I know that in iOS 13 the window is accessed in the SceneDelegate, but this project's target is iOS 12.4. The SceneDelegate has been preceeded by @available(iOS 13.0, *).

How can I access UIWindow from the appDelegate or how can I make the above code work?

I'm working with Xcode 11 and Swift 4.2.

Marwen Doukh
  • 1,946
  • 17
  • 26
cesarcarlos
  • 1,271
  • 1
  • 13
  • 33
  • When run under iOS 13, you must not use the app delegate for the window or the root view controller. Only do that under iOS 12. Use the scene delegate for setting up the window and root controller under iOS 13. – rmaddy Oct 25 '19 at 00:40
  • This is targetted for ios 12 but I still get window = nil in the app delegate. – cesarcarlos Oct 25 '19 at 00:41
  • 1
    By targeted do you mean the Deployment Target is iOS 12? That still means your app will use the scene delegate when run under iOS 13. See https://stackoverflow.com/a/58208876/1226963 for a useful template/example. – rmaddy Oct 25 '19 at 00:42
  • Thanks this helped me a lot. I was under the impression that SceneDelegate simply was ignored if the target was older than iOS 13. I moved the code to SceneDelegate and now it works. – cesarcarlos Oct 25 '19 at 01:10
  • Keep in mind that when your app is run under iOS 12, none of the scene delegate code is used. Test on both versions. – rmaddy Oct 25 '19 at 01:12

0 Answers0