2017-07-06 175 views
-1

試圖創建Firebase登錄/註冊,但無論我在電子郵件文本字段中輸入什麼內容,都會輸出錯誤「電子郵件地址格式不正確」,即使其中包含有效的電郵。此外,我是一個初學者,所以...格式的抱歉。這是代碼:Firebase電子郵件身份驗證iOS:「電子郵件格式不正確」

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

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



     } 
    }