-1

I am using a firebase based application, in which I would like to skip the login page if the user is already logged in. My application initially starts on the login page, so here is my code to segue immediately if the user is already logged in.

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if FIRAuth.auth()?.currentUser != nil {
            DispatchQueue.main.async {
                self.performSegue(withIdentifier: self.loginSegue, sender: nil)
            }
        }
    }

This works as intended, HOWEVER, for a SPLIT SECOND, you can see the login page before it segues properly. My segue has its animation off, and I would like the main VC to be presented without the sign in being seen on screen at all.

  • have you set your login viewController as an initial viewController in storyboard? if yes then remove it and you need to set one flag in appDelegate's didLaunch method and check if the user is already logged in then set your home VC as a root view controller other wise send to login VC. – Pankaj K. Aug 10 '17 at 17:25
  • And see https://github.com/mattneub/RegistrationExample – matt Aug 10 '17 at 17:27
  • Nevermind, I fixed the issue – Darshan Kalola Aug 10 '17 at 18:37

2 Answers2

1

App Delegate would be the perfect place to do this work. Make this test in applicationDidFinishLaunching method and just set your main view controller as the first view controller of your navigation controller and then make your navigation controller as rootViewController.

Shubham
  • 763
  • 5
  • 20
  • Thanks! I've got it working, but now when I press a logout button, the sign in screen has a navigation bar which I don't want. This is because I am segueing with the logout button instead of dismissing which I had previously done. EDIT ***I FIXED It by using a present modally segue, thank you again! – Darshan Kalola Aug 10 '17 at 18:35
  • welcome! Please mark the answer as accepted if it solved your problem. – Shubham Aug 11 '17 at 06:39
0

Presenting First ViewController based on Firebase Current Auth state can be done in two ways :-

  1. Check for the users authentication sate in the AppDelegate : NOT ADVISABLE, as it might take long to have a result due to faulty/slow internet connection or some other issue, leading which might not even start your app.
  2. Create a separate Loading page as your FirstViewController(I don't mean the launchStoryboard) which checks the auth state of the current user using that app : ADVISED, as here not only you can check your users auth state via async call and then segue to a desired viewController, but also you will have LoadingViewController present to the user which can also warn them about the network connection if its faulty.
Dravidian
  • 9,945
  • 3
  • 34
  • 74