【问题标题】:Checking for user authentication error codes with Swift 3使用 Swift 3 检查用户身份验证错误代码
【发布时间】:2016-09-25 20:46:25
【问题描述】:

在旧版本的 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 中的第一块代码完成的工作?

【问题讨论】:

    标签: ios swift firebase firebase-authentication


    【解决方案1】:

    使用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 的指导。在我最近转换为 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")
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-12-12
        • 2022-12-15
        • 1970-01-01
        • 1970-01-01
        • 2017-01-04
        • 2013-01-08
        • 1970-01-01
        相关资源
        最近更新 更多