0

I want to initialize window inside appDelegate to show specific ViewController depend on some cases. Now I have this code:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        window = UIWindow()

        let rootNavigationController = UIViewController()
        window?.rootViewController = rootNavigationController
        window?.rootViewController?.view.backgroundColor = .green

        window?.makeKeyAndVisible()
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }

}

I'm using XCode11 and have created new project. SceneDelegate file I removed cause it hasn't effect on this. Also removed Main from Info.plist and from deployment info

As result on device I see black screen but debugger show me that rootNavigationController as should be image from debugger

How fix it or implement this logic for XCode11?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
dev2019
  • 49
  • 1
  • 6
  • I am doing the same on my project, but we do have the Main storyboard set on the .plist file. Probably that does some internal configuration? We later on `didFinishLaunchingWithOptions` check some user properties and instantiate the proper controller and set it as root (same as you do). My project was created with xCode 10.3 – Lucho Sep 25 '19 at 14:20
  • 1
    Why did you remove the SceneDelegate? That is what is used in iOS 13 for creating the main window and root view. – rmaddy Sep 25 '19 at 14:21
  • See https://stackoverflow.com/questions/57451496/why-is-manually-setup-root-view-controller-showing-black-screen for the proper solution. – rmaddy Sep 25 '19 at 14:26

1 Answers1

1

Solution: 1)Inside manifest(plist) file remove Storyboard Name field 2)inside SceneDelegate.swift implement:

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 }
        window = UIWindow(windowScene: windowScene)

        let rootNavigationController = UIViewController()
        window?.rootViewController = rootNavigationController
        window?.rootViewController?.view.backgroundColor = .green
        window?.makeKeyAndVisible()
    }

And it will work for ios 13 , if you want support ios 12 and lower you need also implement this logic in AppDelegate

dev2019
  • 49
  • 1
  • 6