ios - 在新的 Firebase 和 Swift 中处理错误

标签 ios swift error-handling firebase firebase-authentication

我正在尝试使用 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 添加错误处理的正确语法是什么!

最佳答案

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

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

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

关于ios - 在新的 Firebase 和 Swift 中处理错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39054162/

相关文章:

c#专业错误处理

batch-file - 如何修复该批处理文件? (打字机效果)

ios - iPhone A13 Bionic 破解 Streaming SDK(编码)

ios - 如何通过 SKScene 播放视频

ios - 模块 'SocketIO' 未使用库进化支持进行编译;使用它意味着无法保证框架的二进制兼容性

swift - 如何消除导致此线程 1 : EXC_BAD_ACCESS error? 的错误

python - Python-小小的初学者代码困惑

iphone - 命令/Developer/usr/bin/ibtool 失败,退出代码为 10

iphone - iOS开发,在当前 View 之上显示UIScrollView区域

ios - 解析 iOS SDK 1.8.5 导致 try throw 和 PFObject 条件转换错误