【问题标题】:Firebase sign out not working in SwiftFirebase 退出在 Swift 中不起作用
【发布时间】:2016-10-22 22:54:09
【问题描述】:

我正在使用最新的 Firebase API (3.2.1),并且我正在使用此代码检查用户是否已登录:

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

    if(self.navigationController != nil){
        self.navigationController!.setNavigationBarHidden(true, animated: true)
    }

    if(FIRAuth.auth() != nil){
        self.performSegueWithIdentifier("loginSuccessSegue", sender: self)
    }
}

换句话说,如果存在身份验证对象,我将切换到其他控制器。在那个控制器上,我有退出按钮,它正在像这样退出:

do{
    try FIRAuth.auth()?.signOut()
    self.performSegueWithIdentifier("logoutSegue", sender: self)
}catch{
    print("Error while signing out!")
}

我在此操作中没有收到错误,但是当我切换到登录控制器时,此身份验证对象存在,并且我再次切换回带有数据的控制器。我还尝试在 auth 中检查当前用户对象,它存在且有效。

有人知道我如何正确退出吗?

【问题讨论】:

  • 我刚刚在自己的应用程序中实现了这一点,并得到了 Firebase 的 Wiley 的一点帮助。您可以按照我在this 答案中的描述添加一个侦听器。

标签: swift firebase firebase-authentication


【解决方案1】:

2020 ...

do { try Auth.auth().signOut() }
catch { print("already logged out") }

通常,在您的“基本”屏幕上,类似 ...

func logoutUser() {
    // call from any screen
    
    do { try Auth.auth().signOut() }
    catch { print("already logged out") }
    
    navigationController?.popToRootViewController(animated: true)
}

【讨论】:

    【解决方案2】:

    尝试使用:

    try! FIRAuth.auth()!.signOut()
    

    这是我在 IBAction 中的代码,它工作得很好:

    try! FIRAuth.auth()!.signOut()     
    if let storyboard = self.storyboard {
        let vc = storyboard.instantiateViewControllerWithIdentifier("firstNavigationController") as! UINavigationController
            self.presentViewController(vc, animated: false, completion: nil)
        }
    

    Swift 4.2 更新#

    try! Auth.auth().signOut()
    
    if let storyboard = self.storyboard {
                let vc = storyboard.instantiateViewController(withIdentifier: "firstNavigationController") as! UINavigationController
                self.present(vc, animated: false, completion: nil)
            }
    

    【讨论】:

    • 我不明白我会用这个实现什么,我试过了,但它仍然不起作用。
    • 这对我有用。我所能推荐的就是创建一个新项目并设置一个基本的登录/注销,就像你在真实项目中所做的那样。如果还是不行,那就是你使用的方式有问题,否则就看看有什么不同。
    • @Damien Bannerot 即使你成功运行了试试! FIRAuth.auth()!.signOut(),.currentUser.uid 将继续存储在钥匙串中。如果有人知道如何解决此问题,请随时发布正确答案。
    • @bogdanbarbulescu:Firebase 使用钥匙串吗?
    • @Damien Bannerot Firebase 身份验证会话在 iOS 钥匙串中的用户设备上持续存在。关于 stackoverflow LINK 有一个解释。您能否发布有关如何删除存储在钥匙串中的会话的示例代码?当用户登录时,什么都不做,因为会话是由 Firebase 自动保存的,当用户注销时,删除 Firebase 会话。在 viewDidLoad 之后,如果我检查 .currentUser.uid ,结果应该是 nil。
    【解决方案3】:

    我想补充一点,我遇到了同样的问题,并且一直在尝试其他人建议的代码的各种组合。

    对我来说,问题是当我在情节提要中设置注销按钮时,我还通过从按钮拖动到登录视图控制器的控件创建了一个连接,认为这就是我想要它做的事情.

    事实证明,我的退出代码从未运行,因为触发的 Segue 返回登录控制器,因此它返回登录屏幕并立即返回到我的第二个视图控制器,因为用户从未注销。

    所以最后这对我有用:

    do {
        try Auth.auth().signOut()
        self.dismiss(animated: true, completion: nil)
        } catch let err {
            print(err)
    }
    

    但只有在之后我删除了我无意中创建的segue。

    【讨论】:

      猜你喜欢
      • 2017-03-17
      • 2017-02-07
      • 1970-01-01
      • 2020-03-07
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      • 2019-03-09
      相关资源
      最近更新 更多