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

标签 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 中第一段代码的功能吗?

最佳答案

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

关于ios - 使用 Swift 3 检查用户身份验证错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39691680/

相关文章:

ios - 如何判断用户关闭推送通知还是删除应用

ios - 也将 Parse iOS SDK 用于网络应用程序

arrays - 从 Swift 中的数组值中获取第一个字符

swift 2 : Spawning a new sprite every 30 seconds

android - 如何处理 Firebase Auth account-exists-with-different-credential 错误

ios 7 在 vi​​ewDidAppear 中显示警报 View 导致 View 不出现

ios - XCTestCase waitForExpectationsWithTimeout :handler: throwing EXC_BAD_ACCESS when expectation is not fulfilled

ios - MGLPolyLine 颜色更改不起作用 - Swift 4、Mapbox

firebase - 应用程序图标徽章 - Firebase 推送通知/FCM - Flutter

javascript - Firebase firestore 时间戳查询不返回任何文档