简体   繁体   中英

UIViewController not presented from AppDelegate

I am trying to present a sign up/ log in ViewController if the user is not instantiated yet, but for some reason the ViewController does not want to present itself, it is presenting a black page with nothing on it. This is what I have in my AppDelegate.swift file, and it should present the ViewController if the user has not signed up yet (which I have not), I set a break point in the VC's ViewDidLoad() which does not hit. Anyone have an idea of what's going wrong?

var window: UIWindow?

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

        if Auth.auth().currentUser == nil {
            let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
            let authVC = storyboard.instantiateViewController(identifier: "AuthVC")
            window?.makeKeyAndVisible()
            window?.rootViewController?.present(authVC, animated: true, completion: nil)

        }
        return true
    }

You need to create window before present anything to that if you deleted scene delegate

window  = UIWindow(frame: UIScreen.main.bounds)
if let window = window {
window.rootViewController = UIViewController()
window.makeKeyAndVisible()
window.rootViewController?.present(authVC, animated: true, completion: nil)
}

For SceneDelegate

 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 navigationController = UINavigationController(rootViewController: UIViewController()) // instead of UIViewController() give it your application root controller... which you want to show when you dismiss presented controller 
     window?.rootViewController = navigationController  
     window?.makeKeyAndVisible()
     window?.rootViewController?.present(authVC, animated: true, completion: nil)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM