Ios 如何在Firebase身份验证中正确使用addStateDidChangeListener?

Ios 如何在Firebase身份验证中正确使用addStateDidChangeListener?,ios,swift,firebase,firebase-authentication,Ios,Swift,Firebase,Firebase Authentication,我正在尝试创建registerVC,在用户填写文本字段并按下register按钮后,我尝试使用Firebase身份验证创建用户 Auth.auth().createUser(withEmail: email, password: password) { (user, error) in // ... } 这是注册按钮按下时的代码 @IBAction func signUpButtonDidPressed(_ sender: Any) { print("000")

我正在尝试创建registerVC,在用户填写文本字段并按下register按钮后,我尝试使用Firebase身份验证创建用户

Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
  // ...
}
这是注册按钮按下时的代码

@IBAction func signUpButtonDidPressed(_ sender: Any) {
        print("000")
        SVProgressHUD.show(withStatus: "Harap Tunggu")

        print("a")

        userEmail = emailTextField.text!
        userPassword = passwordTextField.text!

        print("b")

        Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (user, error) in
            print("c")


            print("d")



            if let user = user {
                print("e")

                user.sendEmailVerification(completion: { (error) in
                    print("f")
                    print("g")


                    let fullName = "\(self.firstNameTextField.text!) \(self.lastNameTextField.text!)"
                    let emailOfTheUser = "\(self.emailTextField.text!)"

                    print("h")

                    let userKMViaEmail = UserKM(uid: user.uid, fullname: fullName, email: emailOfTheUser )

                    print("i")


                    print("111")

                    // save user basic information in NSUserDefault
                    self.saveUserKMBasicDataInUserDefault(userKM: userKMViaEmail)
                     print("2222")

                    self.showAlert(alertTitle: "BErhasil", alertMessage: "email dikirim", actionTitle: "OK")

                    // the user need to read the alert first, after tap the action the user will be moved to LoginUsingEmailVC
//                    self.showCustomAlert(alertTitle: "Periksa Email Anda!", alertMessage: "Assalamualaikum, Mohon Ikuti link yang ada pada email yang kami kirimkan. Harap tunggu 15-30 menit apabila email kami belum juga sampai.", actionTitle: "Login")
//
                     print("3333")
                    SVProgressHUD.dismiss()

                     print("444")

                })
            }

        }




    }
在app delegate中,我编写代码来决定如果用户已经登录,是否直接转到主选项卡栏,否则当应用启动时,用户将被发送到欢迎屏幕VC

以下是我的应用程序委托中的代码

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        FirebaseApp.configure()
        checkUserLogin()


        // to track the user default info.plist location in the device (simulator) for checking purpose.
        print("The Location of info.plist of User Default: \(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String)")


        return true
    }

}




extension AppDelegate {

    // MARK: - HELPER METHODS

    func checkUserLogin() {
        // to check whether the user has already logged in or not


        SVProgressHUD.show()
        Auth.auth().addStateDidChangeListener { (auth, user) in

            if user == nil {
                SVProgressHUD.dismiss()
                self.goToWelcomeVC()
            } else {
                SVProgressHUD.dismiss()
                self.goToMainTabBar()
            }

            // if user is not nil then automatically go to maintab bar (initialVC is indeed main tab bar controller)
            SVProgressHUD.dismiss()
        }
    }



    func goToMainTabBar () {
         print("XXXXXXXX")
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let mainTabBar = storyboard.instantiateViewController(withIdentifier: "mainTabBar")
        window?.rootViewController = mainTabBar

    }

    func goToWelcomeVC () {
        let storyboard = UIStoryboard(name: "Welcome", bundle: nil)
        let login = storyboard.instantiateViewController(withIdentifier: "WelcomeVC")
        window?.rootViewController = login

    }


}
这是我调试区域的打印结果,我们可以看到AppDelegate中的XXXXX被自动触发

正如我们所看到的,在按下注册按钮之后,
Auth.Auth().createUser()
被触发。当触发
Auth.Auth().createUser()
时,AppDelegate中的
Auth.Auth().addStateDidChangeListener
也将被触发

但不幸的是,当AppDelegate中的
Auth.Auth().addStateDidChangeListener
被触发时,它将使我的应用程序自动转到主选项卡栏

在这一点上,我希望我留在SignUpVC,而不是MainTabBarVC

也许我在编写下面的代码时犯了错误,因为当createUser()被激活时,
addStateDidChangeListener()
中的用户基本上不再是零

Auth.auth().addStateDidChangeListener { (auth, user) in

            if user == nil {
                SVProgressHUD.dismiss()
                self.goToWelcomeVC()
            } else {
                SVProgressHUD.dismiss()
                self.goToMainTabBar()
            }

            // if user is not nil then automatically go to maintab bar (initialVC is indeed main tab bar controller)
            SVProgressHUD.dismiss()
        }

但是,如果用户已经登录,我也希望用户在主选项卡栏中,否则,如果他们还没有登录,请转到欢迎屏幕?

当/如果用户成功注册,他也会自动登录。这就是为什么您的
addStateDidChangeListener
会触发并将用户带到
MainTabBar
。因此,代码正在按预期方式执行。