【问题标题】:Segue after new user creation using firebase not being performed -Swift在未执行使用 firebase 创建新用户后进行 Segue -Swift
【发布时间】:2017-08-26 23:22:26
【问题描述】:

但是,我遇到了两个 segues 的问题,因为我在实现每个 segues 时使用了相同的逻辑,所以我将继续将所有内容浓缩为一个问题。基本上,我正在尝试在使用 firebase 执行 createUser 函数并执行同样由 Firebase 处理的 login 函数之后初始化一个 segue,但是,当单击 SignUpLogin 按钮时,似乎没有响应被调用的segue。下面是每个@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)
            }
            }

            }
        })
}

@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)

                }

            }


        }
    })

乍一看是否有任何明显的逻辑错误,或者有没有更好的方法来实现这些函数的 segue?

【问题讨论】:

    标签: ios iphone swift firebase firebase-authentication


    【解决方案1】:
    • 将导航控制器嵌入故事板的第一个视图
    • 然后尝试使用此代码:-

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

    然后以类似的方式更改您的其他代码块。

    【讨论】:

      猜你喜欢
      • 2019-04-17
      • 2016-11-23
      • 2016-08-03
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多