firebase errorCodes将不会显示

问题描述 投票:1回答:1

我尝试将错误代码设置为登录,例如用户想要输入他的个人资料但键入错误的密码,他会收到通知他输入了错误的密码...但如果我尝试它只会带我没有登录到正常的feed页面,所以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以来有一些变化,但我没有发现任何相关内容

ios swift firebase firebase-authentication
1个回答
1
投票

1.正确获取错误代码

为了正确获取错误代码,您需要将Swift Error转换为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)
}
© www.soinside.com 2019 - 2024. All rights reserved.