如果我注销并登录然后无法第一次登录,为什么?在 swift

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

我的应用流程如下

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

我在项目中有 SceneDelegate,所以尝试下面的代码

SceneDelegate代码:

  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 }
}

登录VC代码:

 @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 注销按钮代码:

  @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)
    
}

使用上面的代码我可以自动登录,但是一旦我注销并且如果我登录然后它不会去 homeVC.. 如果我停止并且 运行 然后显示 homeVC 为什么, 我哪里错了.. 请帮忙.

我将更改登录如下:

删除 SceneDelegate 中的所有内容:

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

在您的 SignInViewController 中更改如下:

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)
}

在你的故事板中创建一个名为“toHomeVC”的转场。您的故事板应如下所示:

然后在 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
}

最后,如果您需要使用 UITabarController,只需像这样嵌入您的 HomeVc:

你的 Storyboard 应该是这样的:

如果您决定将它嵌入 UITabBarController,您必须像这样隐藏后退按钮:

self.tabBarController?.navigationItem.hidesBackButton = true