Firebase 注销在 Swift 中不起作用
Firebase sign out not working in Swift
我正在使用最新的 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 对象存在并且我再次切换回带有数据的控制器。我还尝试检查 auth 中的当前用户对象,它存在且有效。
有人知道我如何正确退出吗?
尝试使用:
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)
}
我想补充一点,我遇到了同样的问题,并且一直在尝试其他人建议的代码的各种组合。
我的问题是,当我在情节提要中设置注销按钮时,我还通过控件从按钮拖动到我的登录视图控制器来创建连接,认为这就是我想要的要做。
事实证明,我的注销代码从来没有 运行 因为触发了 Segue 回到登录控制器,所以它返回到登录屏幕并立即进入我的第二个视图控制器,因为用户从未注销.
所以最后这对我有用:
do {
try Auth.auth().signOut()
self.dismiss(animated: true, completion: nil)
} catch let err {
print(err)
}
但仅之后我删除了无意中创建的转场。
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)
}
我正在使用最新的 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 对象存在并且我再次切换回带有数据的控制器。我还尝试检查 auth 中的当前用户对象,它存在且有效。
有人知道我如何正确退出吗?
尝试使用:
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)
}
我想补充一点,我遇到了同样的问题,并且一直在尝试其他人建议的代码的各种组合。
我的问题是,当我在情节提要中设置注销按钮时,我还通过控件从按钮拖动到我的登录视图控制器来创建连接,认为这就是我想要的要做。
事实证明,我的注销代码从来没有 运行 因为触发了 Segue 回到登录控制器,所以它返回到登录屏幕并立即进入我的第二个视图控制器,因为用户从未注销.
所以最后这对我有用:
do {
try Auth.auth().signOut()
self.dismiss(animated: true, completion: nil)
} catch let err {
print(err)
}
但仅之后我删除了无意中创建的转场。
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)
}