【问题标题】:How to solve the problem with Firebase listener, which open everywhere?如何解决随处开放的 Firebase 监听器的问题?
【发布时间】:2021-08-23 19:29:00
【问题描述】:

我有一些简单的应用程序,它有身份验证。 最近的代码检查您是否已经输入:

 func signingManager(){
    Auth.auth().addStateDidChangeListener { [weak self] (auth, user) in
        guard let self = self else {return}
        if user != nil {
            self.showNextVC()
            print("You are already entered")
        }
    }
}

当你第一次打开应用程序时它就可以工作,如果你输入 func "showNextVC" 将打开下一个 VC。 同时我有带有代码的登录按钮:

    @IBAction func logInTapped(_ sender: UIButton) {
    guard let email = emailTextField.text, let password = passwordTextField.text, email != "", password != "" else {
        displayWarningLabel(withText: "info is incorrect")
        return
    }
    Auth.auth().signIn(withEmail: email, password: password, completion: { [weak self] (user, error) in
        if error != nil {
            self?.displayWarningLabel(withText: "error occured")
            return
        }
        if user != nil {
            self?.showNextVC()
            print("Congratulations, you have successfully logged in!")
        }
        self?.displayWarningLabel(withText: "no such user")
    }
)}

现在关于问题:如果我单击“登录”按钮,首先触发“signingManager ()”方法和它的“showNextVC”,然后才触发“logInTapped”方法本身,然后再次触发“showNextVC”。 结果,我有 2 个 VC 和两条消息: “您已经进入”和 “恭喜,您已成功登录!” 我究竟做错了什么?谢谢!

【问题讨论】:

    标签: ios swift firebase-authentication


    【解决方案1】:

    由于您正在侦听身份验证状态更改,因此您无需在 signIn(withEmail:, password:) 的完成回调中处理 self?.showNextVC()。该代码应该只出现在addStateDidChangeListener 的回调中。

    或者,您可以:

    1. 使用addStateDidChangeListener 初步检测用户是否已经登录。
    2. 在状态更改的回调中:
      1. 通过调用removeAuthStateDidChangeListener删除监听器
      2. 启动显式登录流程,并像现在一样致电signIn(withEmail:, password:)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 2021-07-24
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      相关资源
      最近更新 更多