解析电子邮件和用户名登录

时间:2016-12-27 18:11:27

标签: ios iphone swift parsing

我尝试使用用户名或电子邮件登录用户。现在我的代码只能用户名登录用户。我使用" 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 {
     }

1 个答案:

答案 0 :(得分: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._%+-]+@[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._%+-]+@[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
    }

}