小编典典

处理新Firebase和Swift中的错误

swift

我正在尝试使用swift和firebase在iOS项目中创建用户按钮时添加错误处理:

这是按钮的代码:

     @IBAction func Register(sender: AnyObject) {

    if NameTF.text == "" || EmailTF.text == "" || PasswordTF.text == "" || RePasswordTF == "" || PhoneTF.text == "" || CityTF.text == ""
    {
        let alert = UIAlertController(title: "عذرًا", message:"يجب عليك ملىء كل الحقول المطلوبة", preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
        self.presentViewController(alert, animated: true){}

    } else {

        if PasswordTF.text != RePasswordTF.text {
            let alert = UIAlertController(title: "عذرًا", message:"كلمتي المرور غير متطابقتين", preferredStyle: .Alert)
            alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
            self.presentViewController(alert, animated: true){}

        } else {


            FIRAuth.auth()?.createUserWithEmail(EmailTF.text!, password: PasswordTF.text!, completion: { user, error in
                print(error)

                if error != nil {

                    let errorCode = FIRAuthErrorNameKey

                    switch errorCode {
                    case "FIRAuthErrorCodeEmailAlreadyInUse":
                        let alert = UIAlertController(title: "عذرًا", message:"الإيميل مستخدم", preferredStyle: .Alert)
                        alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
                        self.presentViewController(alert, animated: true){}

                    case "FIRAuthErrorCodeUserNotFound":
                        let alert = UIAlertController(title: "عذرًا", message:"المستخدم غير موجود", preferredStyle: .Alert)
                        alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
                        self.presentViewController(alert, animated: true){}

                    case "FIRAuthErrorCodeInvalidEmail":
                        let alert = UIAlertController(title: "عذرًا", message:"الإيميل غير صحيح", preferredStyle: .Alert)
                        alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
                        self.presentViewController(alert, animated: true){}

                    case "FIRAuthErrorCodeNetworkError":
                        let alert = UIAlertController(title: "عذرًا", message:"خطأ في الاتصال بالانترنت", preferredStyle: .Alert)
                        alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
                        self.presentViewController(alert, animated: true){}

                    default:
                        let alert = UIAlertController(title: "عذرًا", message:"خطأ غير معروف", preferredStyle: .Alert)
                        alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
                        self.presentViewController(alert, animated: true){}



                    }


                } else {

                    FIRAuth.auth()?.signInWithEmail(self.EmailTF.text!, password: self.PasswordTF.text!, completion: { (user: FIRUser?, error: NSError?) in
                        if let error = error {
                            print(error.localizedDescription)
                        } else {

                           self.ref.child("UserProfile").child(user!.uid).setValue([
                                "email": self.EmailTF.text!,
                                "name" : self.NameTF.text!,
                                "phone": self.PhoneTF.text!,
                                "city" : self.CityTF.text!,
                                ])
                            print("Sucess")
                          //  self.performSegueWithIdentifier("SignUp", sender: nil)

                        }
                    })

                } //else
            })

        } //Big else


    } //Big Big else
}


}//end of

我不确定switch语句中错误的语法是否正确!

因为当我在模拟器中对其进行测试时,它始终会给我提供默认情况,这是未知错误!+我在文档中找不到语法:https
:
//firebase.google.com/docs/auth/ios/errors

因此,使用新的firebase和swift添加错误处理的正确语法是什么?


阅读 267

收藏
2020-07-07

共1个答案

小编典典

实际上,我已经为此苦苦挣扎了很长时间,并且发现了问题所在。我已经尝试过上面答案中发布的代码,并且error.code行给了我一个错误。它确实与error._code一起工作。换句话说,对Paul的原始答案稍加修改即可。这是我的最终代码(不过,我将针对所有错误对其进行编辑):

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

    switch errCode {
        case .errorCodeInvalidEmail:
            print("invalid email")
        case .errorCodeEmailAlreadyInUse:
            print("in use")
        default:
            print("Create User Error: \(error)")
    }    
}
2020-07-07