【问题标题】:Why my createUser Method Trigger addStateDidChangeListener Method in appDelegate?为什么我的 createUser 方法在 appDelegate 中触发 addStateDidChangeListener 方法?
【发布时间】:2018-03-26 06:31:16
【问题描述】:

在应用代理中,我编写代码来决定如果用户已经登录,用户是否会直接进入主标签栏,否则在应用启动时用户将被发送到欢迎屏幕 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。 ?

【问题讨论】:

  • 当您创建一个用户时,该用户会自动登录,所以我认为如果您只想在验证电子邮件后转到下一个屏幕,您将不得不稍微更改您的逻辑。您可以查看this answer

标签: ios swift firebase firebase-authentication


【解决方案1】:

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

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 func 中,添加以下内容:

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

【讨论】:

    猜你喜欢
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    相关资源
    最近更新 更多