Frederik Nissen
Frederik Nissen

Reputation: 39

Starting at a certain view if user is logged in iOS Swift

I am currently making a small login app using Firebase. I am currently having problems with my login page.

When my users already are logged in, when they open the app, I want to change the initial view controller, so that the user can go straight to the homepage.

So my question is, what line of code do I have to perform in order to do this?

override func viewDidLoad() {
    super.viewDidLoad()
    if FIRAuth.auth() ? .currentUser ? .uid == nil {
        notLoggedIn()
    }
}
func notLoggedIn() {
    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "Startpage") as!ViewController
    self.present(nextViewController, animated: true, completion: nil)
}

Upvotes: 1

Views: 4453

Answers (2)

profiile_samir
profiile_samir

Reputation: 301

You can use this line of codes.

Keep in mind that you should add storyboard reference with identifier named respectively for your need - goToLogin - in my case.

Hope It'll be helpful for anyone.

override func viewDidLoad() {
    super.viewDidLoad()
    Auth.auth().addStateDidChangeListener { auth, user in
       if let user = user {
         // User is signed in.
         print("user signed in")
         //Add the rest of the code here because after passig the caluses 
         //  viewdidload will call another funxtions to it can crash
         
       } else {
         // User not signed in
         self.performSegue(withIdentifier: "goToLogin", sender: Any?.self)
         
       }
     }
}

Upvotes: 2

Jordan
Jordan

Reputation: 4212

There's a couple of ways you can do this. If you really want to change the initial view controller, you would want to NOT set an initial view controller in your storyboard, then in your app delegate's application(_:didFinishLaunchingWithOptions:) implementation, you would create a new Window object and set whichever view controller on it you want to present as the rootViewController. Then, you would call makeKeyAndVisible on that window object. Note that if you do it this way, you'll have to separately handle the case when they log out if you want to display your login window again. In that case you would just do the same thing again: make a new window object with your new ViewController object as the rootViewController and present it.

Another option is to check if they are logged in in your initial view controller's viewDidLoad method and then present your login screen if they aren't. This is what I do in one of applications where the app needs some data, either by logging into an account or manually adding it, before it can do anything.

EDIT:

Here's what my viewDidLoad, etc. looks like (note that mine project is in Objective-C, so I'm just kinda guessing without actually testing it what the correct Swift syntax is. You might need to make some adjustments) You have to dispatch the present call to the main queue because in viewDidLoad you (probably) don't have everything in order yet to actually present a new view controller (I did this quite a long time ago, so I don't recall exactly why it has to be dispatched, but because of the fact that we're already in the process of presenting the current view controller, it makes sense that you wouldn't be able to present another one at the same time. Maybe someone else can weigh in on this, because I really don't remember anymore.):

override func viewDidLoad() {
    super.viewDidLoad()
    if (!userLoggedIn) {
        showLoginScreen()
    }
}

func showLoginScreen() {
    let loginViewController = storyboard?.instantiateViewController(withIdentifier: "Startpage") as! ViewController
    DispatchQueue.main.async {
        present(loginViewController, animated: true, completion: nil)
    }
}

Upvotes: 4

Related Questions