0

I've created a new project using Xcode 11. I'm used to creating my UIWindow's root view controller programmatically and I first noticed this new SceneDelegate file. After some research I found an article describing how to create root view controller using this new UIScene API and it seems to work properly. However, I couldn't find a way to do the same thing for lower iOS versions because AppDelegate class now does not have a window property anymore. So my question is how do I create a root view controller programmatically for other iOS versions?

Alexandr Chekel
  • 382
  • 2
  • 15
  • `UIScene` is iOS13+ only, so if you want to support older OS-es, you'll need to manually add the `UIWindow` code. – Cristik Oct 05 '19 at 09:59
  • See https://github.com/rmaddy/XcodeTemplates for an updated Single View App template that creates either a code or storyboard based app project that supports iOS 12 and 13. It supports both Swift and Objective-C. – rmaddy Oct 05 '19 at 16:21

1 Answers1

1

Here's the code if someone faces the same thing.

iOS 13 workflow

Code taken from this article.

In your SceneDelegate file:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let windowScene = (scene as? UIWindowScene) else { return }
        self.window = UIWindow(frame: windowScene.coordinateSpace.bounds)
        self.window?.windowScene = windowScene

        let controller = UIViewController()
        let navigationController = UINavigationController(rootViewController: controller)

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


iOS 12 and lower

In your AppDelegate file:

    var window: UIWindow?

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

        if #available(iOS 13.0, *) { return true }

        self.window = UIWindow(frame: UIScreen.main.bounds)

        let controller = UIViewController()
        let navigationController = UINavigationController(rootViewController: controller)

        self.window?.rootViewController = navigationController
        self.window?.makeKeyAndVisible()

        return true
    }                         


It is important to hold UIWindow in a variable in order for it to show up.

Alexandr Chekel
  • 382
  • 2
  • 15
  • 1
    This answer is only a start. It is not complete. `didFinishLaunchingWithOptions` will be called under both iOS 12 and 13. But you do not want to create the window or root view controller there under iOS 13. – rmaddy Oct 05 '19 at 16:19
  • See https://stackoverflow.com/a/58208876/1226963 for a proper setup. – rmaddy Oct 05 '19 at 16:23