为什么我的createUser方法在appDelegate中触发addStateDidChangeListener方法?

时间:2018-03-26 06:31:16

标签: ios swift firebase firebase-authentication

在app delegate中,我编写代码来判断用户是否已经登录时用户是否会直接进入Main Tab Bar,否则用户将在应用程序启动时被发送到Welcome Screen VC。

这是我的App 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()时,它会在我的应用委托中自动触发Auth.auth().addStateDidChangeListener()方法,因此它会让我的应用自动转到主标签栏,此时我希望我的应用仍然在RegistrationVC中,因为用户必须先验证他们的电子邮件,然后登录才能进入主标签栏。

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

1 个答案:

答案 0 :(得分:0)

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

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) in行上方,添加:

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

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

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