简体   繁体   中英

Segue after new user creation using firebase not being performed -Swift

I am having issues with two segues however, since I used the same logic in the implementation of each of these segues I'm going to go ahead and condense everything into one question. Basically, I am trying to initialize a segue after performing createUser function with firebase and after performing login function also handled by Firebase, however, when the SignUp and Login buttons are clicked there seems to be no response to the segue being called. Below is the code for each of the @IBActions

 @IBAction func signUpComplete(_ sender: Any) {
    FIRAuth.auth()?.createUser(withEmail: signUpEmailField.text!, password: signUpPasswordField.text!, completion: { (user: FIRUser?, error) in
        if self.signUpEmailField.text == "" || self.signUpPasswordField.text == "" {
                let alert = UIAlertController(title: "Oops!", message: "A valid E-mail and Password are Required", preferredStyle: UIAlertControllerStyle.alert)
                alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
                    self.present(alert, animated: true, completion: nil)



        if FIRAuthErrorCode.errorCodeEmailAlreadyInUse != nil {

            let emailExists = UIAlertController(title: "Oops!", message: "A user with this E-Mail already exists!", preferredStyle: UIAlertControllerStyle.alert)
            emailExists.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
            self.present(emailExists, animated: true, completion: nil)


            }

        else {

            DispatchQueue.main.async () {
                self.performSegue(withIdentifier: "signUpSucceededSegue", sender: self)
            }
            }

            }
        })
}

and

@IBAction func loginButton(_ sender: Any) {

    FIRAuth.auth()?.signIn(withEmail: emailLoginField.text!, password: passwordLoginField.text!, completion: { (user: FIRUser?, error) in
            if self.emailLoginField.text == "" || self.passwordLoginField.text == ""{

                let alert = UIAlertController(title: "Alert", message: "E-Mail is Required", preferredStyle: UIAlertControllerStyle.alert)
                alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
                self.present(alert, animated: true, completion: nil)

            if FIRAuthErrorCode.errorCodeUserNotFound != nil {

                let invalidLogin = UIAlertController(title: "Alert", message: "E-Mail/Password Incorrect", preferredStyle: UIAlertControllerStyle.alert)
                invalidLogin.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
                self.present(invalidLogin, animated: true, completion: nil)

            }

        else{


                DispatchQueue.main.async () {

                    self.performSegue(withIdentifier: "loginSucceededSegue", sender: self)

                }

            }


        }
    })

are there any glaring logical errors at first glance or is there a better way to implement a segue into these functions?

  • Embed a Navigation Controller to the very first view of your storyboard
  • Then Try using this code:-

     @IBAction func signUpComplete(_ sender: Any) { if self.signUpEmailField.text == "" || self.signUpPasswordField.text == "" { let alert = UIAlertController(title: "Oops!", message: "A valid E-mail and Password are Required", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil)) self.present(alert, animated: true, completion: nil) }else{ FIRAuth.auth()?.createUser(withEmail: self.signUpEmailField!.text, password: self.signUpPasswordField!.text, completion: { (user, err) in if let ErrorCode = FIRAuthErrorCode(rawValue: err!._code){ switch ErrorCode { case .errorCodeEmailAlreadyInUse : let emailExists = UIAlertController(title: "Oops!", message: "A user with this E-Mail already exists!", preferredStyle: UIAlertControllerStyle.alert) emailExists.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil)) self.present(emailExists, animated: true, completion: nil) break default : break } }else{ let nextView = self.navigationController!.storyboard!.instantiateViewController(withIdentifier: "signUpSucceededSegue") self.navigationController!.pushViewController(nextView, animated: true) } }) } } 

Then alter your other code-block in the similar fashion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM