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

问题描述

我的应用流程如下

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

enter image description here

我在项目中有 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”的转场。您的故事板应如下所示:

enter image description here

enter image description here

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

enter image description here

你的故事板应该是这样的:

enter image description here

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

self.tabBarController?.navigationItem.hidesBackButton = true 

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...