ios - 为什么我的createUser方法会触发appDelegate中的addStateDidChangeListener方法?

标签 ios swift firebase firebase-authentication

在应用程序委托(delegate)中,我编写代码来决定如果用户已经登录,用户是否直接进入主选项卡栏,否则在应用程序启动时用户将被发送到欢迎屏幕 VC。

这是我的应用程序委托(delegate)中的代码。

@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

        }


    }

我正在尝试创建registerVC,在用户填写文本字段并按下注册按钮后我尝试使用Firebase身份验证创建用户,当用户按下registerVC中的注册按钮时将触发代码

Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
  // ...
}

当触发 Auth.auth().createUser() 时,它会自动触发我的应用程序委托(delegate)中的 Auth.auth().addStateDidChangeListener() 方法,因此它使我的应用程序自动转到主选项卡栏,此时我希望我的应用程序仍处于 RegistrationVC 中,因为用户必须先验证其电子邮件,然后登录才能转到主选项卡栏。

触发Auth.auth().createUser(withEmail: email,password:password)时如何阻止用户进入MainTab Bar?但我还想保留如果用户已经登录则用户将直接进入主选项卡栏的功能,否则当应用程序启动时用户将被发送到欢迎屏幕 VC。 ?

最佳答案

您现在可能已经解决了您的问题,但此答案仅供记录。 我遇到了同样的问题,我是这样解决的:

1 - 将 if user == nil { 的内部替换为:

let registeration = UserDefaults.standard.bool(forKey: "registering")
if(!registeration) {
     SVProgressHUD.dismiss()
     self.goToWelcomeVC()
}

2 - 在 Auth.auth().createUser(withEmail: email,password:password) { (user, error) 行中,添加:

UserDefaults.standard.set(true, forKey: "registering")

3 - 在 mainTabBar VC 的 viewDidLoad 函数中,添加以下内容:

UserDefaults.standard.set(false, forKey: "registering")

关于ios - 为什么我的createUser方法会触发appDelegate中的addStateDidChangeListener方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49485258/

相关文章:

ios - 使用google帐户登录后如何执行segue?

ios - UIKitCore 崩溃 [UIViewController viewDidMoveToWindow :shouldAppearOrDisappear:] iOS 12 only

ios - Xcode 7.2 的 Add Size Class Customization 在哪里?

ios - 奇怪的加速度计行为(CoreMotion)

ios - WorldPay AccessCheckoutSDK IOS "generateSession"给出 "Identity is invalid"错误

java - 如何应用循环从 Firebase 检索所需的数据?

ios - 数字到 firebase 迁移 iOS 电话号码输入

ios - Xcode 6.1.11 - 存档应用程序没有图标

swift - Swift 中的正确样式 : Guard Statement

android - 如何在不影响实时数据库的情况下使用 firebase 数据库测试我的应用程序?