Firebase签出不在Swift工作

时间:2016-06-21 11:49:48

标签: swift firebase firebase-authentication

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

for each in (response['fixtures']):
    pprint (each['id'])

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

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对象存在并且我再次切换回带有数据的控制器。我还尝试检查auth中的当前用户对象,它是否存在且有效。

任何人都知道我如何正确退出?

3 个答案:

答案 0 :(得分:30)

尝试使用:

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)
        }

答案 1 :(得分:5)

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 :(得分:4)

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

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

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

所以最后这对我有用:

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

但只有之后我删除了我在不知不觉中创建的segue。

相关问题