Firebase电子邮件身份验证iOS:“电子邮件格式错误”

时间:2017-07-06 00:04:25

标签: ios swift email firebase firebase-authentication

尝试创建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!)
        }

    }
}
)}
}
}
)
}
}

1 个答案:

答案 0 :(得分:0)

我的参考代码看看

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



        }
    }