处理新Firebase和Swift中的错误 [英] Handling Errors in New Firebase and Swift

查看:234
本文介绍了处理新Firebase和Swift中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里是按钮的代码:

  @IBAction func寄存器(sender:AnyObject){

如果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 !,密码:PasswordTF.text !,完成:{user,错误
print(error)

如果错误!=无{

let errorCode = FIRAuthErrorNameKey

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

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

caseFIRAuthErrorCodeInvalidEmail:
let aler t = UIAlertController(title:عذرا,message:الإيميلغيرصحيح,preferredStyle:.Alert)
alert.addAction(UIAlertAction(title:نعم,style:.Default){_in})
self.presentViewController(alert,animated:true){}

caseFIRAuthErrorCodeNetworkError:
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){}


$ b}


} else {

FIRAuth.auth()?。
中的signInWithEmail(self.EmailTF.text !,密码:self.PasswordTF.text !,完成:{(user:FIRUser?,error:NSError?)if 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(Sign 发件人:无)

}
})

} // else
})

} / / Big else


} // Big Big else
}


} //

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



因为当我在模拟器中测试它时,总会给我一个未知的错误!
+
我在文档中找不到语法:
https://firebase.google.com/docs/auth/ios/errors



那么,使用new添加错误处理的正确语法是什么firebase和迅速!

解决方案

实际上,我只是为此付出了很多时间,发现问题所在。我已经尝试了上面的答案发布的代码和error.code行给了我一个错误。它确实与error._code工作。换句话说,保罗原来的回答只是稍加修改。这是我的最终代码(我将编辑所有错误):

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

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


I'm trying to add error handling in creating user button in iOS project using swift and firebase:

Here's the code for the button:

     @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

I'm not sure if the syntax of the errors in switch statement is correct or not!

Because when I tested it in the simulator it always gives me the defualt case which is unknown error! + I could not find the syntax in the documentation: https://firebase.google.com/docs/auth/ios/errors

So, What's the correct syntax to add error handling using new firebase and swift!

解决方案

I've actually just struggled with this for quite a bit of time and found what the issue was. I've tried the code posted in an answer above and the error.code line gave me an error. It did work with error._code though. In other words, credit for the original answer to Paul with a slight modificaiton. Here's my final code (I will edit it for all errors though):

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)")
    }    
}

这篇关于处理新Firebase和Swift中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆