2016-11-03 61 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) 
    } 


}