Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

ios - Handling Errors in New Firebase and Swift

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...