【问题标题】:Firebase 3.6.0 (Auth) - Detecting the specific error using Swift 3.0Firebase 3.6.0 (Auth) - 使用 Swift 3.0 检测特定错误
【发布时间】:2017-02-09 17:00:31
【问题描述】:

我正在尝试找出如何检测某个错误。假设登录失败,我想检查错误是否是说输入的帐户不存在,然后告诉查看者。如果可能,所有其他错误也是如此。

在 Parse 中,我会检查 error.code 是否等于某个数字,不确定它是否与 Firebase 相同或类似。

【问题讨论】:

  • stackoverflow.com/questions/37449919/… 此处已回答问题,发布了相同的代码 dravidian
  • 事先搜索时找不到任何东西,感谢您指出这一点。
  • @Rialcom 这不是同一个答案。在 swift3 中,您需要以._code 访问您的错误。

标签: ios swift firebase swift3 firebase-authentication


【解决方案1】:

这是带有通知的新格式

Auth.auth().createUser(withEmail: email, password: password) { (user: User?, error) in

if error != nil {

    if let errCode = AuthErrorCode(rawValue: error!._code) {

        switch errCode {
        case .invalidEmail:

            print("invalid email")
            // Create an alert message
            let alertMessage = UIAlertController(title: "Invalid Email", message: "Please check the entered email address", preferredStyle: .alert)
            // Attach an action on alert message
            alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                alertMessage.dismiss(animated: true, completion: nil)
            }))
            // Display the alert message
            self.present(alertMessage, animated: true, completion: nil)

        case .emailAlreadyInUse:

            print("in use")
            // Create an alert message
            let alertMessage = UIAlertController(title: "Existed Email", message: "The email existed in our database, login instead of registering", preferredStyle: .alert)
            // Attach an action on alert message
            alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                alertMessage.dismiss(animated: true, completion: nil)
            }))
            // Display the alert message
            self.present(alertMessage, animated: true, completion: nil)

            case .weakPassword:

                print("password is weak")
                // Create an alert message
                let alertMessage = UIAlertController(title: "Password is weak", message: "Use upper and lower characters along with numbers", preferredStyle: .alert)
                // Attach an action on alert message
                alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                    alertMessage.dismiss(animated: true, completion: nil)
                }))
                // Display the alert message
                self.present(alertMessage, animated: true, completion: nil)

        default:
            print("Other error!")
        }

    }
}

【讨论】:

    【解决方案2】:

    使用这个:-

    if let errCode = FIRAuthErrorCode(rawValue: err!._code) {
    
                    switch errCode {
                    case .errorCodeInvalidEmail:
                        print("invalid email")
                    case .errorCodeEmailAlreadyInUse:
                        print("in use")
                    default:
                        print("Other error!")
                    }
    
                }
    

    err 是从 firebase 收到的错误

    【讨论】:

      猜你喜欢
      • 2016-09-20
      • 1970-01-01
      • 2015-08-01
      • 2017-01-18
      • 2017-03-25
      • 2016-09-18
      • 1970-01-01
      • 2017-02-20
      • 2016-10-15
      相关资源
      最近更新 更多