问题描述
Auth.auth().signIn(withEmail: emailTextField.text!, password: passwordTextField.text!)
{ (user, error) in
if error != nil {
print(error!)
self.warningLabel.isHidden = false;
self.passwordTextField.text = "";
} else {
print("Log in succesful")
self.performSegue(withIdentifier: "welcomeSeg", sender: self)
}
}
每当我登录或注册用户时,我只会打印通用警告标签,而不是实际问题.我打印了我收到的错误,它太冗长而无法显示给用户.
Whenever I sign in or sign up a user I just print a generic warning label instead of the actual issue. I print the error I receive and it's too verbose to show to the user.
错误域= FIRAuthErrorDomain代码= 17008电子邮件地址格式错误." UserInfo = {NSLocalizedDescription =电子邮件地址格式错误.,error_name = ERROR_INVALID_EMAIL}
Error Domain=FIRAuthErrorDomain Code=17008 "The email address is badly formatted." UserInfo={NSLocalizedDescription=The email address is badly formatted., error_name=ERROR_INVALID_EMAIL}
有什么方法可以获取错误代码,以便我可以更详细地了解错误消息吗?我仔细阅读了文档,但未提出任何建议.
Is there any way to fetch the error code so I can be more specific with my error messages? I've looked through the documentation but have been unsuccessful in coming up with anything.
推荐答案
我建议根据收到的错误创建一个AuthErrorCode
对象(由Firebase SDK提供),并根据需要使用它.如果我没记错的话,AuthErrorCode
是一个枚举,其中包含.wrongPassword
,.invalidEmail
等情况.
I would recommend creating an AuthErrorCode
object (provided by the Firebase SDK) from the error you receive and using that as you see fit. If I remember correctly, AuthErrorCode
is an enum with cases like .wrongPassword
, .invalidEmail
, etc.
一个简单的伪代码示例:
A simple pseudocode example:
if error != nil {
if let error = AuthErrorCode(rawValue: error?.code) {
switch error {
case .wrongPassword:
// do some stuff with the error code
}
}
我也感到你的痛苦.我发现在进行更改时,Swift SDK文档会滞后很多.
Also, I feel your pain. I've found that the Swift SDK documentation lags quite a bit when changes come along.
这篇关于如何从Firebase获取错误代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!