Dej*_*jan 13 firebase swift 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)
}
if(FIRAuth.auth() != nil){
self.performSegueWithIdentifier("loginSuccessSegue", sender: self)
}
}
Run Code Online (Sandbox Code Playgroud)
换句话说,如果存在auth对象,我将切换到其他控制器.在那个控制器上,我有一个退出按钮,它正在这样做:
do{
try FIRAuth.auth()?.signOut()
self.performSegueWithIdentifier("logoutSegue", sender: self)
}catch{
print("Error while signing out!")
}
Run Code Online (Sandbox Code Playgroud)
我没有得到这个操作的错误,但当我切换到登录控制器时,这个auth对象存在,我再次切换回带有数据的控制器.我还尝试检查auth中的当前用户对象,它是否存在且有效.
任何人都知道我如何正确退出?
Dam*_*rot 36
尝试使用:
try! FIRAuth.auth()!.signOut()
Run Code Online (Sandbox Code Playgroud)
这是我在IBAction中的代码,它运行得很好:
try! FIRAuth.auth()!.signOut()
if let storyboard = self.storyboard {
let vc = storyboard.instantiateViewControllerWithIdentifier("firstNavigationController") as! UINavigationController
self.presentViewController(vc, animated: false, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
try! Auth.auth().signOut()
if let storyboard = self.storyboard {
let vc = storyboard.instantiateViewController(withIdentifier: "firstNavigationController") as! UINavigationController
self.present(vc, animated: false, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
Fat*_*tie 10
do { try Auth.auth().signOut() }
catch { print("already logged out") }
Run Code Online (Sandbox Code Playgroud)
通常,在您的“基本”屏幕上,类似...
func logoutUser() {
// call from any screen
do { try Auth.auth().signOut() }
catch { print("already logged out") }
navigationController?.popToRootViewController(animated: true)
}
Run Code Online (Sandbox Code Playgroud)
小智 6
我只想补充一点,就是我遇到了同样的问题,并且一直在尝试其他人建议的各种代码组合。
原来对我来说,问题是当我在情节提要板上设置“注销”按钮时,我还通过控件从按钮拖到我的登录视图控制器上来创建了一个连接,认为这就是我想要的。
事实证明,由于触发Segue返回登录控制器,我的签出代码从未运行过,因此由于用户从未注销,因此它返回了登录屏幕,并立即回到了我的第二个视图控制器。
所以最后这对我有用:
do {
try Auth.auth().signOut()
self.dismiss(animated: true, completion: nil)
} catch let err {
print(err)
}
Run Code Online (Sandbox Code Playgroud)
但是只有在删除了我不经意间创建的序列之后。
| 归档时间: |
|
| 查看次数: |
20661 次 |
| 最近记录: |