swiftxcodewarningsview-hierarchy

Blank screen first time launching app xcode


I am having a little issue with logging into an app the first time with a blank screen and I am getting the warning "Attempt to present on whose view is not in the window hierarchy!" After I close and relaunch, the views appear fine. I believe it has something to do with the rootViewController but not sure... Thanks in advance for any help or direction!

App delegate

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

        var window: UIWindow?
        var ref:FIRDatabaseReference?
        var databaseHandle:FIRDatabaseHandle?

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

            window = UIWindow(frame: UIScreen.main.bounds)
            window?.makeKeyAndVisible()
            window?.rootViewController = MainNavigationController()

            FIRApp.configure()
            ref = FIRDatabase.database().reference()

            return true
        }

Navigation controller as rootViewController

    class MainNavigationController: UINavigationController {

        var segmentedController: UISegmentedControl!

        override func viewDidLoad() {
            super.viewDidLoad()

            let vc1 = TravelersFeedVC()
            let vc2 = ProfileVC()

            if isLoggedIn() {
                // assume user is logged in
                let homeController = HomeController()
                viewControllers = [homeController]
                homeController.firstViewController = vc1
                homeController.secondViewController = vc2


            } else {
                perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
            }
        }

        fileprivate func isLoggedIn() ->  Bool {
            return UserDefaults.standard.isLoggedIn()
        }

        func showLoginController() {
            let loginController = LoginController()
            present(loginController, animated: true, completion: {
                // perhaps do something here later
            })
        }
    }

// log in function called

    func finishLoggingIn() {
        let rootViewController = UIApplication.shared.keyWindow?.rootViewController
        guard let mainNavigationController = rootViewController as? MainNavigationController else { return }

        let vc1 = TravelersFeedVC()
        let vc2 = ProfileVC()

        if isLoggedIn() {
            // assume user is logged in
            let homeController = HomeController()
            mainNavigationController.viewControllers = [HomeController()]
            homeController.firstViewController = vc1
            homeController.secondViewController = vc2

        } else {
            perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
        }
        UserDefaults.standard.setIsLoggedIn(value: true)
        dismiss(animated: true, completion: nil)
    }

Solution

  • Ok, so my last answer was wrong (i deleted it), the things is that in app there is a keywindow, which is your navcontroller, and you cannot present anything on this one untill it will load its subviews (even if it hasn't any), and that would happend on viewdidappear, so you should put your code from viewdidload there.