2

到目前为止,我们已经使用以下语法检查了带有用户名和密码的登录凭据。

logInWithUsernameInBackground

 [PFUser logInWithUsernameInBackground:[UsernameField.text lowercaseString] password:PasswordField.text block:^(PFUser* user, NSError* error){

但现在我的要求是:

用户必须使用 email 和 UserName 中的任何一个登录

我们需要检查用户名/电子邮件和密码

如何做到这一点?

4

2 回答 2

16
PFQuery *query = [PFUser query];
    [query whereKey:@"email" equalTo:UsernameField.text];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
        if (objects.count > 0) {

            PFObject *object = [objects objectAtIndex:0];
            NSString *username = [object objectForKey:@"username"];
            [PFUser logInWithUsernameInBackground:username password:PasswordField.text block:^(PFUser* user, NSError* error){
            }];
        }else{
            [PFUser logInWithUsernameInBackground: UsernameField.text password:PasswordField.text block:^(PFUser* user, NSError* error){
            }];

        }


    }];
于 2013-09-03T14:45:21.203 回答
0

斯威夫特 3 / 4

Metin Say 的回答转换为 Swift。

static func signIn(username: String, password: String, onSuccess: @escaping ([PFObject]) -> (), onError: @escaping (String) -> ()) {
        let query = PFQuery(className: "User")
        query.whereKey("email", equalTo: username)
        query.findObjectsInBackground(onSuccess: { (objects) in
            if (objects.count > 0) {
                guard let object = objects.first as? PFUser, let username = object.username else{
                    return
                }
                PFUser.logInWithUsername(inBackground: username, password: password) { (user, error) in
                    if let error = error {
                        onError(error.localizedDescription)
                    } else {
                        onSuccess(objects)
                    }
                }
            }
            else{
                PFUser.logInWithUsername(inBackground: username, password: password) { (user, error) in
                    if let error = error {
                        onError(error.localizedDescription)
                    } else {
                        onSuccess(objects)
                    }
                }
            }
        }, onError(error.localizedDescription))
    }
于 2018-10-10T14:28:55.337 回答