firebase errorCodes不会显示

时间:2019-02-15 05:03:54

标签: ios swift firebase firebase-authentication

我尝试为登录设置错误代码,例如,用户想要输入个人资料,但输入了错误的密码,那么他会收到通知,提示他输入了错误的密码...但是如果我尝试输入只是将我带到了正常的供稿页面而没有登录,因此errorCode甚至都没有显示出来...

    Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in

        if error != nil {
            print(error ?? "")
            if let errorCode = AuthErrorCode(rawValue: error!._code) {
                switch errorCode {
                case .invalidEmail:
                    print("invalid email")
                    let alertMessage = UIAlertController(title: "Email is invalid", message: "Use upper and lower characters along with numbers", preferredStyle: .alert)
                    alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                        alertMessage.dismiss(animated: true, completion: nil)
                    }))
                    self.present(alertMessage, animated: true, completion: nil)

                case .accountExistsWithDifferentCredential:
                    print("wrong credential")
                    let alertMessage = UIAlertController(title: "Wrong Information", message: "The Account exists with different Credentials than entered from you", preferredStyle: .alert)
                    alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                        alertMessage.dismiss(animated: true, completion: nil)
                    }))
                    self.present(alertMessage, animated: true, completion: nil)

                case .wrongPassword:
                    print("wrong password")
                    let alertMessage = UIAlertController(title: "Password is wrong", message: "Please enter the correct password for your account", preferredStyle: .alert)
                    alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                        alertMessage.dismiss(animated: true, completion: nil)
                    }))
                    self.present(alertMessage, animated: true, completion: nil)

                default:
                    print("Other error!")
                    let alertMessage = UIAlertController(title: "Ouhhhh", message: "It seems like something went wrong. Please try again later.", preferredStyle: .alert)
                    alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                        alertMessage.dismiss(animated: true, completion: nil)
                    }))
                    self.present(alertMessage, animated: true, completion: nil)
                }

            }
            return
        } else {
        //successfully logged in our user
        self.messagesController?.fetchUserAndSetupNavBarTitle()
        self.performSegue(withIdentifier: "showJobs", sender: nil)
        SVProgressHUD.dismiss()
        }})

有人可以帮我吗?自从Firebase 5以来,似乎有些变化,但是我没有找到任何关于

的信息

1 个答案:

答案 0 :(得分:1)

1。正确获取错误代码

为了正确获取错误代码,您需要将Swift 错误转换为 NSError

您可以使用以下代码:

if error != nil {
     let ns_error = error! as NSError
     print(ns_error.code)
}

2。在主线程的完成块上执行与UI相关的任务

case .invalidEmail:
    print("invalid email")
    DispatchQueue.main.async {
               // Update UI elements here
               let alertMessage = UIAlertController(title: "Email is invalid", message: "Use upper and lower characters along with numbers", preferredStyle: .alert)
               alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                   alertMessage.dismiss(animated: true, completion: nil)
               }))
               self.present(alertMessage, animated: true, completion: nil)
    }

您需要处理主线程中的所有警报和与UI相关的情况。

尝试在显示警报之前添加延迟

func delayExecution(seconds delay:Double, closure:@escaping ()->()) {
    DispatchQueue.main.asyncAfter(
        deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure)
}