繁体   English   中英

firebase errorCodes 不会出现

[英]firebase errorCodes won't show up

我尝试为登录设置错误代码,例如用户想要输入他的个人资料但输入了错误的密码,他会收到通知他输入了错误的密码......但如果我尝试它只会给我带来无需登录即可进入正常的提要页面,因此错误代码甚至不显示...

    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.正确获取错误代码

为了正确获取错误代码,您需要将 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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM