PFLogInViewController - 仅使用登录按钮注册和登录

时间:2015-12-08 06:19:08

标签: ios swift parse-platform

我正在自定义Parse PFLogInViewController并试图使其成为登录按钮将触发查询以查看用户名是否已存在。如果确实存在,则正常登录将继续。如果它不存在,那么用户将被注册。

我最初遇到查询的异步性问题,但是我将查询从我的函数中分离出来,这样看起来不是问题。正如你在代码中看到的那样,我复制了logInViewController函数,因此它的第二个版本有一个额外的参数doesExist: Bool

当我测试它时,当我查看打印日志时,逻辑似乎随处可见,但重复的logInViewController函数实际上并未记录用户。

感谢任何建议!

func signUpFunc(username: String, password: String) {

    //this is the sign up function

    print(username)
    print(password)

    print("this is the sign up function")

}

func doesUserExist(username: String, password: String) {

    var query = PFUser.query()
    query!.whereKey("username", equalTo: username)

    query?.findObjectsInBackgroundWithBlock({ (users, error) -> Void in

        if let users = users {

            if users.count == 0 {

                self.signUpFunc(username, password: password)

            } else {

                print ("made it here")

                print(username)
                print(password)



                let testController = LoginViewController()
                self.logInViewController(true, logInController: testController, shouldBeginLogInWithUsername: username, password: password)



            }

        }

    })

}


func logInViewController(logInController: PFLogInViewController, shouldBeginLogInWithUsername username: String, password: String) -> Bool {

    //this function happens on the initial click of the login button

    self.doesUserExist(username, password: password)

    return false
}

func logInViewController(doesExist: Bool, logInController: PFLogInViewController, shouldBeginLogInWithUsername username: String, password: String) -> Bool {

    //this should actually log the user in or give error if user/pass combo is wrong

    print("made it to login")
    print(username)
    print(password)

    return true

}

1 个答案:

答案 0 :(得分:0)

尝试使用凭据登录并使用Parse的响应代码。但我不会因为用户不退出而创建新用户。您最终将获得更多用户,因为用户的登录信息中存在拼写错误并最终会导致新用户。我先问他们。

func trySignIn() {
    // get string from textfiled
    PFUser.logInWithUsernameInBackground("user12", password: "user12") { (user, err) -> Void in
        if let error = err {
            if error.code == 101 {
                let alert = UIAlertController(title: "Error", message: "\(error.localizedDescription) - Should we creat an account for you?", preferredStyle: UIAlertControllerStyle.Alert)
                let signUpAction = UIAlertAction(title: "Sign up", style: UIAlertActionStyle.Default, handler: { UIAlertAction in
                    self.signUpUser("user12", password: "user12")
                })
                let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
                alert.addAction(signUpAction)
                alert.addAction(cancelAction)
                self.presentViewController(alert, animated: true, completion: nil)
            }
        } else {
            print("logged in successfully")
        }
    }
}

func signUpUser(username: String, password: String){
    let userToSignUp = PFUser()
    userToSignUp.username = username
    userToSignUp.password = password
    userToSignUp.signUpInBackgroundWithBlock { (success, err) -> Void in
        if let error = err {
            self.showAlertViewWithError(error)
        } else {
            print("User signed up") // Show message to user
        }
    }
}
相关问题