使用Swift 3检查用户身份验证错误代码

时间:2016-09-25 20:46:25

标签: ios swift firebase firebase-authentication

在旧版本的Swift中,可以使用以下代码检查用户身份验证错误:

 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替换。

FIRAuthErrorNameKey放在FauthenticationError所在位置会导致错误:

cannot call nonfunctiontype String

以下是我正在查看的文档:https://firebase.google.com/docs/auth/ios/errors

如何实现如何使用Swift 3中的第一个代码块完成工作?

2 个答案:

答案 0 :(得分:12)

使用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:
                    }
            }
}

答案 1 :(得分:2)

SWIFT 3

感谢TonyMkenu的指导。在我最近转换为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")
}
相关问题