2016-05-26 128 views
8

我無法確定如何在新版Firebase中讀取FIRAuthErrorNameKey。以下是我到目前爲止,但「let errorCode = FIRAuthErrorNameKey」行不正確。從閱讀Firebase documentation我也嘗試訪問來自userInfo的錯誤代碼,但沒有成功,並沒有想法。正在讀取Firebase身份驗證錯誤(Firebase 3.x和Swift)

// Send request to Firebase to add user to register user 
FIRAuth.auth()?.createUserWithEmail(emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in 

     // Check for errors and respond to user accordingly. 
     if error != nil { 

      let errorCode = FIRAuthErrorNameKey 

      switch errorCode { 

      case "FIRAuthErrorCodeEmailAlreadyInUse": 

       // Add logic accordingly 

      case ...: 

       // Add logic accordingly 

      case default: 

       // Add logic accordingly 
      } 
     } 
}) 

回答

25

試試這個。這對我有用。此外,粘貼到您的項目後。如果您需要查看所有FIRAuthErrorCode代碼。將鼠標懸停在.ErrorCodeInvalidEmail上,然後按下鼠標左鍵,它會向您顯示其餘的內容。

如果您有任何問題,請讓我知道並生病試圖幫助您。祝你好運!

 FIRAuth.auth()?.createUserWithEmail(email, password: password) { (user, error) in 

      if error != nil { 

       if let errCode = FIRAuthErrorCode(rawValue: error!._code) { 

        switch errCode { 
         case .ErrorCodeInvalidEmail: 
          print("invalid email") 
         case .ErrorCodeEmailAlreadyInUse: 
          print("in use") 
         default: 
          print("Create User Error: \(error!)") 
        } 
       } 

      } else { 
       print("all good... continue") 
      } 
     } 
+1

工作就像一個魅力!感謝您的幫助和思考如何找到錯誤代碼。不幸的是,我永遠無法知道如何在Firebase指南頁面(https://firebase.google.com/docs/auth/ios/errors)上使用字符串錯誤代碼,但這確實有用! – Ben

+1

Firebase現在已經在呼叫前刪除了「FIR」。所以它將只是'AuthErrorCode' – theblindprophet

+2

他們似乎也稍微重命名了錯誤枚舉。如果你輸入'case .',那麼你會看到所有的。 – ffritz

1

從代碼片段看來,您似乎正在嘗試在交換機中使用錯誤代碼而不是FIRAuthErrorNameKey。在這種情況下,你想要使用的是回調中返回的NSError對象的錯誤代碼。 用途:

let errorCode = error.code 

這樣你的errorCode變量將包含返回的錯誤代碼,你可以用你的錯誤處理邏輯進行。

+0

感謝您的想法讓我走上正軌。這在技術上不符合我上面的設置,因爲.code返回一個int。它將通過重新編寫我的代碼來匹配錯誤代碼。我無法在Firebase指南中找到這些內容,但如果按照the_pantless_coder概述的方法查看其他答案中的所有錯誤代碼,則可以在此處獲取整型錯誤代碼。謝謝! – Ben

0

我不知道這是否可以幫助,但你可以做到這一點也:

let errcode = FIRAuthErrorCode(rawValue: error!._code) 
    if errcode == FIRAuthErrorCode.errorCodeRequiresRecentLogin{ 

      //do error handling here 

    } 
5

火力地堡已經改變了代碼一點,如果你是新的火力點它會帶你一點時間讓你的代碼工作。我花了將近3個小時來弄清楚發生了什麼事。下面是它現在的樣子:

Auth.auth().createUser(withEmail: email, password: password) { (user: User?, error) in 

     if error != nil { 
     // Handle the error (i.e notify the user of the error) 

      if let errCode = AuthErrorCode(rawValue: error!._code) { 

       switch errCode { 
       case .invalidEmail: 
        print("invalid email") 
       case .emailAlreadyInUse: 
        print("in use") 
       default: 
        print("Other error!") 
       } 

      } 
     } 
8

無答案似乎是跟上時代的,這是我目前做的雨燕上3.X,FirebaseAuth 4.0.0

Auth.auth().signIn(withEmail: email, password: password) { (user, error) in 

     if let error = error as NSError? { 

      guard let errorCode = AuthErrorCode(rawValue: error.code) else { 

       print("there was an error logging in but it could not be matched with a firebase code") 
       return 

      } 

      switch errorCode { 

      case .invalidEmail: 

       print("invalid email") 

       //...