swiftuinavigationcontrollerrootviewcontroller

If i logout and login then unable to login for the first time, why? in swift


my app flow like below

  uinavigationcontroller(is initial viewcontroller) -> loginVC -> homeVC
                                                          

enter image description here

i am having SceneDelegate in project, so trying code like below

SceneDelegate code:

  func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            let userLoginStatus = UserDefaults.standard.bool(forKey: "USER_LOGIN")
              if (userLoginStatus) {
                let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let vc_TabBar = mainStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
                window!.rootViewController = vc_TabBar
                window!.makeKeyAndVisible()
            }
    guard let _ = (scene as? UIWindowScene) else { return }
}

loginVC code:

 @IBAction func loginBtnTapped(_ sender: Any) {
    
    guard  let email = emailTF.text,
           let password = passwordTF.text
    else {
        print("form is not valis")
        return
    }
  //  UserDefaults.standard.set(true, forKey: "USER_LOGIN")

    Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
                if let _eror = error{
                    print(_eror.localizedDescription)
                }else{

                    if let _res = result{
                        print(_res)
                        UserDefaults.standard.set(true, forKey: "USER_LOGIN")

          
                        let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
                        self.navigationController?.pushViewController(vc!, animated: true)
                    }
                }
            }
   }

homveVC logoutButton code:

  @IBAction func logoutBtnTapped(_ sender: Any) {
    UserDefaults.standard.set(false, forKey: "USER_LOGIN") //logging session off

    do{
        try Auth.auth().signOut()
    }catch let logoutError{
        print(logoutError)
    }
  //  UserDefaults.standard.set(false, forKey: "USER_LOGIN") //logging session off

    let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "SignInViewController") as? SignInViewController
    self.navigationController?.pushViewController(vc!, animated: true)
    
}

with the above code i am able to auto login, but once i logout and if i login then its not going to homeVC.. if i stop and run then showing homeVC why, where am i wrong.. pls do help.


Solution

  • I would change the login as follow:

    remove everything in your SceneDelegate:

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

    in your SignInViewController change has follow:

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        userIsloggedin()
    }
    
    func userIsloggedin(){
        let userLoginStatus = UserDefaults.standard.bool(forKey: "USER_LOGIN")
        if (userLoginStatus) {
            self.performSegue(withIdentifier: "toHomeVC", sender: self)
        }
    }
    
    @IBAction func loginBtnTapped(_ sender: UIButton) {
        UserDefaults.standard.set(true, forKey: "USER_LOGIN")
        self.performSegue(withIdentifier: "toHomeVC", sender: self)
    }
    

    create a segue in your storyboard named "toHomeVC". Your storyboard should look like this:

    enter image description here

    enter image description here

    and then change your code in HomeViewController

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.hidesBackButton = true //to hide the back button 
    }
    
    @IBAction func logoutBtnTapped(_ sender: UIButton) {
          UserDefaults.standard.set(false, forKey: "USER_LOGIN") //logging session off
        self.navigationController?.popToRootViewController(animated: true) // or false if you don't want to see the animation
    }
    

    Lastly, if you need yo use UITabarController just embed your HomeVc like so:

    enter image description here

    and you Storyboard should look like this:

    enter image description here

    If you decide to embed it the UITabBarController you must hide the back button like so:

    self.tabBarController?.navigationItem.hidesBackButton = true