2016-09-25 139 views
3

檢查用戶身份驗證錯誤代碼在老版本的雨燕,下面的代碼可以用來檢查用戶身份驗證錯誤:與斯威夫特3

if (error != nil) { 
    // an error occurred while attempting login 
    if let errorCode = FAuthenticationError(rawValue: error.code) { 
     switch (errorCode) { 
     case .UserDoesNotExist: 
      println("Handle invalid user") 
     case .InvalidEmail: 
      println("Handle invalid email") 
     case .InvalidPassword: 
      println("Handle invalid password") 
     default: 
      println("Handle default situation") 
     } 
    } 
} 

FAuthenticationError似乎不存在了,和文檔使它看起來像被替換爲FIRAuthErrorNameKey

FauthenticationError是導致錯誤把FIRAuthErrorNameKey

cannot call nonfunctiontype String 

這裏是我看的文檔:https://firebase.google.com/docs/auth/ios/errors

任何想法如何實現什麼用的一個代碼塊完成在Swift 3中?

回答

11

使用FIRAuthErrorCode - 這是一個int枚舉

enum FIRAuthErrorCode { FIRAuthErrorCodeInvalidCustomToken = 17000, FIRAuthErrorCodeCustomTokenMismatch = 17002, FIRAuthErrorCodeInvalidCredential = 17004, FIRAuthErrorCodeUserDisabled = 17005,

從這裏:https://firebase.google.com/docs/reference/ios/firebaseauth/interface_f_i_r_auth_errors

嘗試使用這樣的:

if (error != nil) { 
    // an error occurred while attempting login 
    if let errCode = FIRAuthErrorCode(rawValue: (error?._code)!) { 
       switch errCode { 
       case .errorCodeEmailAlreadyInUse: 
       ... 
       case .errorCodeInvalidEmail: 
       ... 
       case .errorCodeWrongPassword: 
        } 
      } 
} 
2

SWIFT 3個

感謝TonyMkenu爲可怕的ction。在我最近轉換爲Swift 3的項目中,使用FIRAuthErrorCode要求我使用默認值:

if (error != nil) { 
    // an error occurred while attempting login 
    if let errCode = FIRAuthErrorCode(rawValue: (error?._code)!) { 
       switch errCode { 
        case .errorCodeRequiresRecentLogin: 
         print("There was an error") 
        default: 
         print("Handle default situation") 
        } 
       } 
}else { 
    // no error occurred 
    print("Success") 
}