使用Swift 3检查用户认证错误代码

3

在早期版本的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个回答

13

使用FIRAuthErrorCode - 它是一个整型枚举

枚举类型 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:
                    }
            }
}

3

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接