【问题标题】:Firebase Email Authentication iOS: "The email is badly formatted"Firebase 电子邮件身份验证 iOS:“电子邮件格式错误”
【发布时间】:2017-07-06 00:04:24
【问题描述】:

尝试创建 Firebase 登录/注册,但无论我在电子邮件文本字段中输入什么,它都会打印错误“电子邮件地址格式错误”,即使它是有效的电子邮件。另外我是初学者,所以......对格式感到抱歉。这是代码:

import UIKit
import Firebase
import FirebaseAuth


class SecondViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

      //  FirebaseApp.configure()
        }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBOutlet weak var username_input: UITextField!
    @IBOutlet weak var password_input: UITextField!
    @IBOutlet weak var signupButton: UIButton!
    @IBOutlet weak var segmentControl: UISegmentedControl!
    @IBAction func doSignUp(_ sender: Any) {
    let email = self.username_input.text
    let password = self.password_input.text

      if email != "" && password != ""
        {
            if segmentControl.selectedSegmentIndex == 1 //signup

            {

                Auth.auth().createUser(withEmail: email!, password: password!, completion: { (user, error) in
                    if error == nil {
                        print("You have successfully signed up")
                        // Goes to main page
                        let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
                        self.present(vc!, animated: true, completion: nil)
                        self.performSegue(withIdentifier: "segue", sender: self)
                        let request = Auth.auth().currentUser?.createProfileChangeRequest()
                        request?.displayName = email

                    }
                    else {
                        if (error?.localizedDescription) != nil
                        {

                            //   let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
                            //  let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                            //   alertController.addAction(defaultAction)
                            // self.present(alertController, animated: true, completion: nil)
                            print(error?.localizedDescription as Any)
                            let myError1 = error?.localizedDescription
                            print(myError1!)

                        }
                    }
                })
            }
        }

    if segmentControl.selectedSegmentIndex == 0 {
    //login

    Auth.auth().signIn(withEmail: self.username_input.text!, password: self.password_input.text!, completion: { (user, error) in
    if user != nil
    {
    self.performSegue(withIdentifier: "segue", sender: self)
    //sign in user
    }
    else
    {
    if (error?.localizedDescription) != nil
    {
    _ = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)

    let myError = error?.localizedDescription
    print(myError!)

            }
    else {
        Auth.auth().signIn(withEmail: self.username_input.text!, password: self.password_input.text!, completion: { (user, error) in
            if user != nil
            {
                self.performSegue(withIdentifier: "segue", sender: self)
                //sign in user
            }
            else
            {
                if (error?.localizedDescription) != nil
                {
                    _ = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)

                    let myError = error?.localizedDescription
                    print(myError!)
        }

    }
}
)}
}
}
)
}
}

【问题讨论】:

  • 为我工作我使用我的代码而不是你的代码尝试设置断点并检查崩溃实际发生的位置

标签: ios swift email firebase firebase-authentication


【解决方案1】:

我的代码供参考看看

//Outlets
        @IBOutlet weak var emailTextField: UITextField!
        @IBOutlet weak var passwordTextField: UITextField!

    //Sign Up Action for email
    @IBAction func createAccountAction(_ sender: AnyObject) {

        let email = emailTextField.text
        let pass = passwordTextField.text
        if emailTextField.text == "" {
            let alertController = UIAlertController(title: "Error", message: "Please enter your email and password", preferredStyle: .alert)

            let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alertController.addAction(defaultAction)

            present(alertController, animated: true, completion: nil)

        } else {
            Auth.auth().createUser(withEmail: email!, password: pass!) { (user, error) in

                if error == nil {
                    print("You have successfully signed up")
                    //Goes to the Setup page which lets the user take a photo for their profile picture and also chose a username

                    let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
                    self.present(vc!, animated: true, completion: nil)

                } else {
                    let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)

                    let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                    alertController.addAction(defaultAction)

                    self.present(alertController, animated: true, completion: nil)
                }
            }



        }
    }

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 2018-02-10
    • 2019-08-05
    • 1970-01-01
    • 2021-11-21
    • 2017-08-10
    • 2020-11-22
    • 2019-05-20
    • 1970-01-01
    相关资源
    最近更新 更多