1

When a Firebase token is invalid, I want that the user is redirect to my app loginVC. This is my code:

func userErrorLogout() {

    print("user error logout")

    let storyboard = UIStoryboard(name: "Main", bundle: nil);
    let viewController: ViewController = storyboard.instantiateViewController(withIdentifier: "loginVC") as! ViewController;

    let rootViewController = self.window!.rootViewController as! UINavigationController;
    rootViewController.pushViewController(viewController, animated: true);
}

I get an error on "let rootViewController:.... = 2019-11-27 13:03:49.364864+0100 Geo[25133:2193886] Fatal error: Unexpectedly found nil while unwrapping an Optional value: file

Since SWIFT is new to me, I am not able to debug this message. How do I perform the redirection?

ProjektWeinheim
  • 201
  • 3
  • 10
  • [You can check this answer, it's may help you](https://stackoverflow.com/questions/25256353/swift-pushviewcontroller-from-appdelegate-rootviewcontroller-navigationcontro) – Halil İbrahim ŞENGÜL Nov 27 '19 at 12:21
  • make sure , your `self.window!.rootViewController` is `UINavigationController ` . Got to Storyboard and add Initial ViewController as `UINavigationController` – Dhiru Nov 27 '19 at 12:39

4 Answers4

1

you need to use the below code :

let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController: ViewController = storyboard.instantiateViewController(withIdentifier: "loginVC") as! ViewController;


let window = (UIApplication.shared.delegate as! AppDelegate).window!

DispatchQueue.main.async {
        window.rootViewController = viewController
}
MhmdRizk
  • 1,591
  • 2
  • 18
  • 34
0

You need to assign the value for your rootViewController. Try this:

func userErrorLogout() {

print("user error logout")

let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController: ViewController = storyboard.instantiateViewController(withIdentifier: "loginVC") as! ViewController;
let nav = UINavigationController(rootViewController: viewController)
self.window?.rootViewController = nav

}

Keshu R.
  • 5,045
  • 1
  • 18
  • 38
0

You need to set the viewcontroller to window. You can do like below. This code also has transition animation it gives fade to transition.

    // MARK: WINDOW SET
func gotoIntro() {
    UIView.transition(with: window!, duration: 0.5, options: .transitionCrossDissolve, animations: {
        UIView.performWithoutAnimation({
            let introStoryboard = UIStoryboard(name: "Intro", bundle: nil)

            let navigationController = introStoryboard.instantiateInitialViewController() as! UINavigationController

            self.window?.rootViewController = navigationController
        })
    }, completion: nil)
}
Alihan
  • 628
  • 4
  • 10
0

As of iOS 13, pushing a VC at the start of a launch is not possible from AppDelegate anymore (Not 100% sure).

You must use SceneDelegate if you wish to present the view from your root. To do so, simply do this inside your SceneDelegate file. I have tested this approach on my own app and is working 100%

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }

        self.window = UIWindow(windowScene: windowScene)
        // check if the token is valid
        if valid  {
            let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "yourVC")
            self.window?.rootViewController = vc
            self.window?.makeKeyAndVisible()
        }
        else {
            // token is invalid
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let rootVC = storyboard.instantiateViewController(identifier: "invalidVC")
            let rootNC = UINavigationController(rootViewController: rootVC)
            self.window?.rootViewController = rootNC
            self.window?.makeKeyAndVisible()
        }
    }
Kevin
  • 291
  • 3
  • 12