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

创建用户Swift时的Firebase错误处理,swift,firebase,error-handling,firebase-authentication,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 errC

我目前正在开发一个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.})

有人能帮我吗?

试着在谷歌控制台中启用identity toolkit API。

试着在谷歌控制台中启用identity toolkit API。

实际上,(在回答前面的消息时),identity toolkit API已经被激活了

问题来自这样一个事实,即生成了一个API密钥,但该密钥被限制在网站请求中

解决方案:因此我刚刚生成了另一个API密钥,将其限制在iOS应用程序中,再次下载.plist,将其放入项目中,并在其中添加API密钥(API密钥作为信息属性,而作为新生成的API密钥值)

实际上,(作为对上一条消息的回复),identity toolkit API已被激活

问题来自这样一个事实,即生成了一个API密钥,但该密钥被限制在网站请求中


解决方案:因此我刚刚生成了另一个API密钥,将其限制在iOS应用程序中,再次下载.plist,将其放入项目中,并在其中添加API_密钥(API_密钥作为信息属性,而作为新生成的API密钥值)

对于创建的新用户,请遵循以下简单代码。请将此代码放入Create Button@IBAction函数中

FIRAuth.auth()?.createUserWithEmail(Username.text!, password: Password.text!
            , completion: {

                user , error in

                if error != nil {
                 print("Something Went to Wrong :(")
                }

                else {

                    print("Account Created Sucessfully ")
                }


        })

对于创建的新用户,请遵循以下简单代码。请将此代码放入Create Button@IBAction函数中

FIRAuth.auth()?.createUserWithEmail(Username.text!, password: Password.text!
            , completion: {

                user , error in

                if error != nil {
                 print("Something Went to Wrong :(")
                }

                else {

                    print("Account Created Sucessfully ")
                }


        })