2016-12-27 104 views
1

我試圖通過使用用戶名或電子郵件登錄用戶。現在我的代碼只能通過用戶名登錄用戶。我正在使用「PFUser.logInWithUsername」,但我也想用用戶電子郵件登錄。我正在嘗試更改我的代碼以允許用戶選擇電子郵件或用戶名。這是我的代碼。解析電子郵件和用戶名登錄

@IBAction func LogInButton(_ sender: AnyObject) { 

    // login functions 
    PFUser.logInWithUsername(inBackground: UsernameOrEmail.text!, password: Password.text!) { (user:PFUser?, error:Error?) -> Void in 
     if error == nil { 

      // remember user or save in App Memeory did the user login or not 

      UserDefaults.standard.set(user!.username, forKey: "username") 
      UserDefaults.standard.synchronize() 

      // call login function from AppDelegate.swift class 
      let appDelegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate 

      // Delay the dismissal by 5 seconds 
      let delay = 1.0 * Double(NSEC_PER_SEC) 
      var time = DispatchTime.now() + Double(Int64(delay))/Double(NSEC_PER_SEC) 
      DispatchQueue.main.asyncAfter(deadline: time, execute: { 

       appDelegate.login() 

      }) 

     } else { 
    } 
+0

你可以使用正則表達式圖案來檢測是否usernameoremail領域是電子郵件,如果不是它必須是一個用戶名 – Starlord

+0

如何將我做 – user7222919

+0

你能告訴我怎麼會看 – user7222919

回答

0

您可以使用正則表達式模式來檢測用戶是否輸入電子郵件。如果檢測返回false,那麼您「知道」用戶輸入了他們的用戶名。

這是我在我的應用程序使用(工作正常):

//A function that returns true or false based on the input. True if email, false if something else. 
func isValidEmail(email:String) -> Bool { 
    let emailRegEx = "[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,}" 
    let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) 
    return emailTest.evaluate(with: email) 
} 


//check if user enters email or not: 
if isValidEmail(email: user!.username){ 
     //email adress detected 
    }else{ 
     //username detected 
    } 

編輯:

正如我理解您的問題上面的函數將解決你的問題。我提供了一些代碼供你測試。 我不知道PFUser能夠做什麼,但我認爲有一個用戶名登錄和電子郵件登錄的功能。

//A function that returns true or false based on the input. True if email, false if something else. 
func isValidEmail(email:String) -> Bool { 
let emailRegEx = "[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,}" 
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) 
return emailTest.evaluate(with: email) 
} 


@IBAction func LogInButton(_ sender: AnyObject) { 

//check if user enters username or email 

    if let usercredentials = UsernameOrEmail.text {  //get the username from textfield 

     if isValidEmail(email: usercredentials){ 
      //user did enter his email as login credential 
      PFUser.logInWithEmail(inBackground: usercredentials, password: Password.text!) { (user:PFUser?, error:Error?) -> Void in 
      if error == nil { 
       //do your login stuff here 
      } else { 

     } 
     }else{ 
      //user did enter his username as login credential 
      PFUser.logInWithUsername(inBackground: usercredentials, password: Password.text!) { (user:PFUser?, error:Error?) -> Void in 
       if error == nil { 
        //do your login stuff here 
       } else { 
      } 
    }else{ 
    //textfield not accessible 
    } 

} 
相關問題