【问题标题】:How to persist Firebase authdata after Force Quit (iOS)如何在强制退出(iOS)后保留 Firebase authdata
【发布时间】:2016-07-10 00:56:46
【问题描述】:

我正在为我的应用使用 Firebase 的电子邮件 + 密码身份验证。登录有效,我使用observeAuthEventWithBlock 检查用户是否已登录 - 为了不显示登录页面。如果我按主页按钮并再次打开应用程序,则没有问题。我遇到的问题是如果我强制退出该应用程序。当我重新打开时,我必须再次登录。

在我显示我的登录代码之前,关于设置的一些注意事项。

  • 有一个 LoginViewController - 未嵌入 - 使用 Storyboard 构建
  • 这已连接到导航控制器
  • 第一个屏幕嵌入其中,应用的其余部分使用此导航控制器。

登录代码:

@IBAction func loginButtonPressed() {
    let userEmail = emailTextField.text

    self.ref.authUser(self.emailTextField.text, password: self.passwordTextField.text, withCompletionBlock: { (error, auth) -> Void in

        guard error == nil else {
            if let errorCode = FAuthenticationError(rawValue: error.code) {
                switch (errorCode) {
                case .EmailTaken:
                    self.displayMessage("Email Error", theMessage: "This email is taken")
                case .InvalidEmail:
                    self.displayMessage("Email Error", theMessage: "This email is invalid")
                case .UserDoesNotExist:
                    self.displayMessage("User Error", theMessage: "A user account for email: \(userEmail!) does not exist")
                case .InvalidPassword:
                    self.displayMessage("Password Error", theMessage: "The password is incorrect")
                case .NetworkError:
                    self.displayMessage("Network Error", theMessage: "Seems like there's a problem with your internet connection")
                default:
                    return
                }
            }
            return //set Unknown Error Alert here
        }
        print("LOGGED IN: segue from loginButtonPressed")
        self.userLoggedIn = true
        print("user is logged in? \(self.userLoggedIn)")
        self.performSegueWithIdentifier("loginLocaleSegue", sender: self)
    })
}

检查用户是否已登录 - 如果是,则转到 navcon,弹出并显示嵌入式视图控制器:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    if self.userLoggedIn.boolValue == true {

        ref.observeAuthEventWithBlock { (authData) -> Void in

            if authData != nil {

 let navCon: UINavigationController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("MainNavigationController") as! UINavigationController
                self.presentViewController(navCon, animated: false, completion: nil)
                navCon.popViewControllerAnimated(false)
                print("user is authenticated: \(authData.providerData["email"] as! String)")

                print("segues from viewDidAppear")

            } else {
                return
            }
        }
    }        
}

我看到与 Firebase 身份验证相关的问题,其中指出 Authdata 默认存储在 Keychain 中,这会导致 Authdata 即使在删除应用程序后仍然存在问题,但我遇到了完全相反的问题。有什么想法吗?

【问题讨论】:

    标签: ios authentication firebase firebase-authentication


    【解决方案1】:

    据我所知,您没有将“self.userLoggedIn = true”写入任何数据库,因此当您让应用程序处于空闲状态时它继续为“True”是有道理的,但一旦您关闭应用程序,然后它变为 nil (无值),这是因为它只是在应用程序打开时在后台冷却,而不是完全关闭。尝试将其写入您的 Firebase 数据库,如本教程中所述,看看是否有帮助。

    https://www.raywenderlich.com/109706/firebase-tutorial-getting-started

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-27
      • 2013-06-18
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 2014-11-26
      • 1970-01-01
      相关资源
      最近更新 更多