【问题标题】:Signing Out of Firebase in Swift在 Swift 中退出 Firebase
【发布时间】:2017-03-17 02:11:54
【问题描述】:

我正在尝试退出 Firebase API,但我似乎不知道如何处理可能发生的任何错误。

Firebase pod 提供了一种退出方法:

FIRAuth.auth()?.signOut()

它标有throws,所以我将它包装在do/try/catch 块中以测试退出过程:

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

我看到 signOut 方法在 Firebase pod 中标记为 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

当我没有允许我检查错误的完成处理程序时,您对处理用户注销的适当方法有什么建议吗?

【问题讨论】:

    标签: swift firebase-authentication


    【解决方案1】:

    根据 Milli 的回答编辑,将发送用户添加回应用程序的初始页面。

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

    【讨论】:

      【解决方案2】:

      你可以像这样捕捉错误

      do
      {
           try Auth.auth().signOut()
      }
      catch let error as NSError
      {
           print(error.localizedDescription)
      }
      

      【讨论】:

        【解决方案3】:

        错误发生的可能性很小,但假设任何事情都不是一件好事。根据文档的声音,它会清除您的钥匙串,这是您能够重新登录到 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)
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 2020-03-07
          • 2016-10-22
          • 1970-01-01
          • 1970-01-01
          • 2020-09-10
          • 1970-01-01
          • 1970-01-01
          • 2017-07-23
          • 1970-01-01
          相关资源
          最近更新 更多