2016-11-03 60 views
0

我試圖退出Firebase API,但似乎無法弄清楚如何處理可能發生的任何錯誤。在Swift中退出Firebase

的火力地堡吊艙提供用於登出的方法:

FIRAuth.auth()?.signOut() 

它標有throws,所以我在do/try/catch塊在測試簽名出過程的方法包裹它:

do { 
    try FIRAuth.auth()?.signOut() 
} catch (let error) { 
    print((error as NSError).code) 
} 

我看到signOut方法標記在火力地堡莢throws,但我看不出它如何處理異步的任何錯誤LY。我嘗試了進入飛行模式,這觸發了網絡請求發生時我的代碼中的網絡錯誤,但是使用signOut方法時,未捕獲該錯誤,因爲我沒有可執行的完成處理程序。 Firebase窗格中的所有其他身份驗證方法都有一個完成處理程序,我可以在其中處理錯誤。

下面是從火力地堡吊艙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 

你有一個適當的方法來處理簽約了用戶的任何建議時,我沒有完成處理程序允許我檢查錯誤?

回答

1

您可以捕獲錯誤這樣

do 
{ 
    try Auth.auth().signOut() 
} 
catch let error as NSError 
{ 
    print(error.localizedDescription) 
} 
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) 


} 
0

從毫的答案補充發送用戶返回到應用程序的初始頁面進行編輯扔了。

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


}