之前询问过这个问题: Firebase sign out not working in Swift Logging a user out with Firebase 3 and Swift still shows the `currentUser` Firebase - iOS Swift: FIRAuth.auth().signOut() not signing out current user
然而,所有答案都不适合我。我有VC 1用户可以登录,在viewdidappear我有一个打印用户打印?.uid。在VC 2中,我有一个按钮,应该注销用户并返回到VC 1. VC 2中的代码:
@IBAction func logOut(_ sender: UIButton) {
if FIRAuth.auth()?.currentUser != nil {
do {
try! FIRAuth.auth()!.signOut()
} catch let error as NSError {
print(error.localizedDescription)
}
}
FBSDKAccessToken.setCurrent(nil)
loggedIn = false
storedValuesData.setValue(nil, forKey: "savedLoginEmail")
storedValuesData.setValue(nil, forKey: "savedLoginPassword")
jumpToVC1()
}
当我回到VC 1时,当用户按下注销按钮时,打印机再次打印用户的UID。但是该用户应该注销,因此打印应该是零。当我确定用户已注销时,如何确保仅显示VC 1?我认为完成块会很好,但我不确定这将如何起作用......
答案 0 :(得分:5)
您不需要完成块,因为该方法是同步的。
@IBAction func logOut(_ sender: UIButton) {
guard FIRAuth.auth()?.currentUser != nil else {
return
}
do {
try FIRAuth.auth()?.signOut()
FBSDKAccessToken.setCurrent(nil)
loggedIn = false
storedValuesData.setValue(nil, forKey: "savedLoginEmail")
storedValuesData.setValue(nil, forKey: "savedLoginPassword")
jumpToVC1()
} catch let error as NSError {
print(error.localizedDescription)
}
}
答案 1 :(得分:0)
AccessToken.current = nil正常工作。因为注销不会撤消它。
do {
try Auth.auth().signOut()
AccessToken.current=nil
return true
} catch {
return false
}