我正在尝试退出Firebase API,但我似乎无法弄清楚如何处理可能发生的任何错误。
Firebase窗格提供了退出方法:
FIRAuth.auth()?.signOut()
它标有throws
,因此我将其打包在do
/ try
/ catch
块中,以测试签出过程:
do {
try FIRAuth.auth()?.signOut()
} catch (let error) {
print((error as NSError).code)
}
我发现signOut
方法在Firebase窗格中标有throws
,但我看不到它如何异步处理任何错误。我尝试进入飞行模式,这会在我的代码中发生网络错误,在网络请求发生的任何其他地方,但使用signOut
方法,该错误未被捕获,因为我没有要执行的完成处理程序。 Firebase pod中的所有其他身份验证方法都有一个完成处理程序,我可以在其中处理错误。
以下是Firebase pod中signOut
方法的文档:
/** @fn signOut:
@brief Signs out the current user.
@param error Optionally; if an error occurs, upon return contains an NSError object that
describes the problem; is nil otherwise.
@return @YES when the sign out request was successful. @NO otherwise.
@remarks Possible error codes:
- @c FIRAuthErrorCodeKeychainError Indicates an error occurred when accessing the keychain.
The @c NSLocalizedFailureReasonErrorKey field in the @c NSError.userInfo dictionary
will contain more information about the error encountered.
*/
open func signOut() throws
当我没有允许我检查错误的完成处理程序时,您是否有任何关于处理用户注销的适当方法的建议?
答案 0 :(得分:1)
你可以像这样抓住错误
do
{
try Auth.auth().signOut()
}
catch let error as NSError
{
print(error.localizedDescription)
}
答案 1 :(得分:0)
错误极不可能发生,但假设任何事情都不好。通过文档的声音,它会消除您的钥匙串,这是您能够重新登录firebase应用程序的唯一方法。从尝试退出我自己的firebase应用程序时,我很惊讶0错误发生。这是原始代码。
@IBAction func logOutTapped(_ sender: Any) {
let firebaseAuth = FIRAuth.auth()
do {
try firebaseAuth?.signOut()
} catch let signOutError as NSError {
print ("Error signing out: %@", signOutError)
}
if Utility.hasFacebook {
let login = FBSDKLoginManager()
login.logOut()
}
if Utility.hasTwitter {
Twitter.sharedInstance().sessionStore.logOutUserID((Twitter.sharedInstance().sessionStore.session()?.userID)!)
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginVC")
self.present(initialViewController, animated: false)
}
无论如何,如果你真的想要一个完成处理程序,那么这就是我快速抛出的东西
func logOut(completion:@escaping(_ errorOccured: Bool) -> Void) {
let firebaseAuth = FIRAuth.auth()
do {
try firebaseAuth?.signOut()
} catch let signOutError as NSError {
completion(true)
}
completion(false)
}
答案 2 :(得分:0)
Edited from Milli's answer to add sending user back to initial page of the app.
// log out
func logout(){
do
{
try Auth.auth().signOut()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let IntroVC = storyboard.instantiateViewController(withIdentifier: "IntroVC") as! introVC
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = IntroVC
}
catch let error as NSError
{
print(error.localizedDescription)
}
}