【问题标题】:How to keep Firebase Connection on iOS when you close and reopen the app?关闭并重新打开应用程序时如何在 iOS 上保持 Firebase 连接?
【发布时间】:2017-11-23 11:47:41
【问题描述】:

我使用 Firebase 创建了一个 iOS 应用。所以当我启动应用程序时,它会要求我登录或注册,这就是我所期望的。但是我想“保存”身份验证,因为当我关闭并重新打开应用程序时,我想避免登录页面。

使用 Firebase 构建的 iOS 应用是否有可能“记住”用户在关闭应用之前已经登录?

【问题讨论】:

    标签: ios firebase swift3 firebase-authentication


    【解决方案1】:

    进行标志登录。当您成功登录后,将其设为 true。login = true 然后更新您的 AppDelegate.swift 文件,即如果登录为 true,那么它将显示您想要的视图。

    【讨论】:

      【解决方案2】:

      Firebase 会自动保留有关经过身份验证的用户的信息。当用户重新启动应用程序时,会读取该信息并(如果可能)恢复其身份验证状态。

      要检测此身份验证状态,您需要实现一个身份验证状态侦听器,如getting the currently signed in user 上的文档中所示:

      handle = Auth.auth().addStateDidChangeListener { (auth, user) in
        // ...
      }
      

      【讨论】:

        【解决方案3】:

        最后感谢 Frank van Puffelen,我实现了这段代码:

        override func viewDidAppear(_ animated: Bool) {
            if Auth.auth().currentUser != nil {
                // User is signed in.
                performSegue(withIdentifier: "goToHome", sender: self)
            } else {
                // No user is signed in.
                // ...
            }
        }
        

        因为 performSegue() 在 vi​​ewDidLoad() 中没有执行

        【讨论】:

          猜你喜欢
          • 2021-06-02
          • 1970-01-01
          • 2017-09-20
          • 2021-11-23
          • 1970-01-01
          • 1970-01-01
          • 2011-07-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多