vvkungx vvkungx - 5 months ago 19
Swift Question

See view in tab bar when login success

I have 4 tabs in Tabbar View Controller in storyboard. When open app tab 1 appear and when tap tab 2-4 I want to show modal view for login. When login success, app will show view in tab 2-4. How can I do this ?

Answer

you did not provide information on how you want to make a check if the user is logged in. but anyway, here is what i did with Firebase

in tab 2-4, check if user is logged in in viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    FIRAuth.auth()?.addAuthStateDidChangeListener({ (auth, user) in
        if let user = user {

        } else {

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let signInViewController = storyboard.instantiateViewControllerWithIdentifier("SignIn")
            self.presentViewController(signInViewController, animated: true, completion: nil)

        }
    })     
}

once the view is loaded, it will check if the user is logged in, if not present the viewcontroller SignIn

once logged in is completed, you just have to dismiss the viewcontroller and it would return to which tab that the user clicked on previously

dismissViewControllerAnimated(true, completion: nil)
Comments