Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase注销未在Swift中工作_Swift_Firebase_Firebase Authentication - Fatal编程技术网

Firebase注销未在Swift中工作

Firebase注销未在Swift中工作,swift,firebase,firebase-authentication,Swift,Firebase,Firebase Authentication,我正在使用最新的Firebase API(3.2.1),我正在使用此代码检查用户是否已登录: override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) if(self.navigationController != nil){ self.navigationController!.setNavigationBarHidden(true, animated: true)

我正在使用最新的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)
    }
}
换句话说,如果存在auth对象,我将切换到其他控制器。在该控制器上,我有一个注销按钮,它执行如下注销操作:

do{
    try FIRAuth.auth()?.signOut()
    self.performSegueWithIdentifier("logoutSegue", sender: self)
}catch{
    print("Error while signing out!")
}
我没有在这个操作中得到错误,但是当我切换到登录控制器时,这个身份验证对象就存在了,并且我再次被切换回控制器并带有数据。我还尝试在auth中检查当前用户对象,并且该对象存在且有效

有人知道I如何正确注销吗?

尝试使用:

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更新#
我只想补充一点,我也有同样的问题,并且一直在尝试其他人建议的各种代码组合

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

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

因此,最终这对我起了作用:

do {
    try Auth.auth().signOut()
    self.dismiss(animated: true, completion: nil)
    } catch let err {
        print(err)
}
但只有在我删除了无意中创建的segue之后。

2020。。。 通常,在你的“基本”屏幕上,像

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

我不明白我用它能达到什么效果,我试过了,但仍然不起作用。这对我来说很有效。我所能推荐的就是创建一个新项目并设置一个基本的登录/注销,就像您在实际项目中所做的那样。如果它仍然不起作用,那么您在使用它的方式上有一些错误,否则只需看看有什么不同。@Damien Bannerot即使您成功运行try!FIRAuth.auth()!。signOut(),则.currentUser.uid将保持存储在keychain中。如果有人知道如何解决此问题,请随时发布正确答案。@bogdanbarbulescu:Firebase是否使用密钥链?@Damien Bannerot Firebase身份验证会话将保留在用户设备的iOS密钥链中。有一个关于stackoverflow的解释。您能否发布一个关于如何删除存储在keychain中的会话的示例代码?当用户登录时,不执行任何操作,因为会话由Firebase自动保存;当用户注销时,删除Firebase会话。在viewDidLoad中执行此操作之后,如果我检查.currentUser.uid,结果应该为零。我刚刚在Firebase的Wiley的帮助下在自己的应用程序中实现了此操作。正如我在回答中所描述的,您可以添加一个侦听器。
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)
}