swift - 创建用户 Swift 时的 Firebase 错误处理

标签 swift firebase error-handling firebase-authentication

我目前正在开发一个 iOS 应用程序,我想实现 Firebase 数据库。

我在处理用户创建过程中的错误时遇到了问题。我总是从开关中得到默认错误,这是我的代码:

FIRAuth.auth()?.createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in
        if (error != nil) {

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

                var alertController = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.alert)
                let okButton = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                    (result: UIAlertAction) -> Void in
                    print("Error transmitted")
                }

                switch errCode {
                case .errorCodeInvalidEmail:
                    print("Invalid email")
                    alertController = UIAlertController(title: "Error", message: "Email syntax is not correct", preferredStyle: UIAlertControllerStyle.alert)
                    alertController.addAction(okButton)
                    self.present(alertController, animated: true, completion: nil)
                case .errorCodeEmailAlreadyInUse:
                    print("Email already in use")
                    alertController = UIAlertController(title: "Error", message: "This email is already in use", preferredStyle: UIAlertControllerStyle.alert)
                    alertController.addAction(okButton)
                    self.present(alertController, animated: true, completion: nil)
                case .errorCodeWeakPassword:
                    print("Password weak")
                    alertController = UIAlertController(title: "Error", message: "Password is too weak. Please choose a password which contains at least 6 characters.", preferredStyle: UIAlertControllerStyle.alert)
                    alertController.addAction(okButton)
                    self.present(alertController, animated: true, completion: nil)
                default:
                    // ALWAYS GET HERE.
                    print(error)
                    alertController = UIAlertController(title: "Error", message: "An unknown error occured.", preferredStyle: UIAlertControllerStyle.alert)
                    alertController.addAction(okButton)
                    self.present(alertController, animated: true, completion: nil)
                }

            }

        } else {
            print("User created")
            let newUser = ["email": self.emailField.text!]
            let firebaseNewUser = self.ref.childByAutoId()
            firebaseNewUser.setValue(newUser)
        }

此外,打印(错误)显示:

Optional(Error Domain=FIRAuthErrorDomain Code=17999 "An internal error has occurred, 
    print and inspect the error details for more information." 
    UserInfo={NSUnderlyingError=0x170257be0 {
        Error Domain=FIRAuthInternalErrorDomain Code=3 "(null)" 
        UserInfo={FIRAuthErrorUserInfoDeserializedResponseKey={
    code = 400;
    errors =     (
            {
        domain = usageLimits;
        message = "Bad Request";
        reason = keyExpired;
    }
    );
    message = "Bad Request";
}}}, 
error_name=ERROR_INTERNAL_ERROR, 
NSLocalizedDescription=An internal error has occurred, print and inspect the 
    error details for more information.})

有人可以帮帮我吗?

最佳答案

尝试在谷歌控制台中启用身份工具包 API。

关于swift - 创建用户 Swift 时的 Firebase 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40484694/

相关文章:

python - 未定义全局变量 - Python

ios - CIDetector,检测到的人脸图像没有显示?

android - Firebase isEmailVerify在Kotlin中不起作用

ios - 自定义颜色 Swift

android - 将 Firebase 存储下载 URL 推送到 Firebase 云 Firestore

android - 错误 :(15, 55) 找不到错误 : cannot access zza class file for com. google.android.gms.common.internal.safeparcel.zza

asp.net - 当错误页面本身卡在asp.net mvc3中时如何处理?

angular - http.get - 如何正确捕获 404

ios - 在哪里放置 View Controller 相关的枚举

ios - 获取有关当前模拟器设备的信息