从 Parse API table 获取数据并设置过滤器

Get the datas from the Parse API table and set filters

我正在使用 Parse SDK。我想从 Parse API table 获取数据,并想将过滤器设置为用户名和密码。我怎样才能做到这一点。下面给出的示例代码是在 NSURLSession 的帮助下完成的,并希望使用 Parse SDK 类.

将其转换为代码
-(void)validateLoginWithUsername:(NSString *)usernam withPassword:(NSString *)pasword
{

NSString *urlString = [NSString stringWithFormat:@baseURL@"/1/login?username=%@&password=%@",usernam,pasword];
NSURL *url = [NSURL URLWithString:urlString];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = @{ @AppId  : @id1,
                                                 @APIKey : @key1 };
self.session = [NSURLSession sharedSession];
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSURLSessionDataTask * dataTask = [self.session dataTaskWithURL:url
                                                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
    {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        if (httpResponse.statusCode == 200)
        {
             NSDictionary *dataToBeTransferedBack = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
            ATTDataLibrary *instance = [ATTDataLibrary getInstanceOfDataLibrary];
            [instance manupulateUserData:dataToBeTransferedBack];
            [singletonInstance getTheSubjectsData];
        }
        else
          {
              dispatch_sync(dispatch_get_main_queue(), ^{
              UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Invalid Login" message:@"Please Check the username and password entered" delegate:self cancelButtonTitle:@"ok"  otherButtonTitles:nil, nil];
            [alert show];
                   });
          }

请在此处勾选 Parse Integration 以将解析添加到您现有的项目中。

请尝试以下代码:

// 如果您使用的是自定义 Table 那么

 PFQuery *query = [PFQuery queryWithClassName:@"TableName"];
 [query whereKey:@"username" equalTo:@"pass your username here"];
 [query whereKey:@"password" equalTo:@"pass your password here"];
 [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
     if(!error){
         if(objects.count>0)
             // Found User
     }
 }];

或者您正在使用 USER table 然后尝试下面的代码

[PFUser logInWithUsernameInBackground:username password:password
    block:^(PFUser *user, NSError *error) {
        if (user) {
            // Found User
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login Error" message:@"Credentials provided were incorrect. Please try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];

        }
    }];

假设您已经在代码中集成了 Parse SDK。 使用以下方法。

-(void)validateLoginWithUsername:(NSString *)usernam withPassword:(NSString *)pasword
{
[PFUser logInWithUsernameInBackground:usernam  password:pasword
                                block:^(PFUser *user, NSError *error) {

                                    if (user) {
                                        // Do stuff after successful login.

                                    } else {
                                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Invalid Login" message:@"Please Check the username and password entered" delegate:self cancelButtonTitle:@"ok"  otherButtonTitles:nil, nil];
                                        [alert show];
                                    }
                                }];
}

您可以使用以下代码使用解析 API 检索记录... 您可以使用 PFQuery

的 where class 应用过滤器
PFQuery *query = [PFQuery queryWithClassName:@"UserDetails"];
    __block int totalNumberOfEntries = 0;
    [query orderByDescending:@"createdAt"];
    [query whereKey:@"UserName" containsString:@"anand"];
    [query whereKey:@"Password" containsString:@"1234"];
    [query countObjectsInBackgroundWithBlock:^(int number1, NSError *error)
    {
        if (!error)
        {
            NSLog(@"------- number-- %d",number1);
            NSLog(@"------- number-- %d",[profileArray count]);
            if (totalNumberOfEntries > [profileArray count])
            {
                NSLog(@"Retrieving data");
                //query.skip=[NSNumber numberWithInt:2300];
                [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                    if (!error) {
                        // The find succeeded.
                        NSLog(@"Successfully retrieved %d records.", objects.count);
                        int j=[profileArray count];
                        if([objects count]>j)
                        {
                            [profileArray addObjectsFromArray:objects];

                            NSLog(@"array count is %d",[profileArray count]);

                            NSLog(@"array is %@",profileArray);                            
                        }
                    }
                    else
                    {
                        // Log details of the failure
                        NSLog(@"Error: %@ %@", error, [error userInfo]);
                    }
                }];
            }

        } else
        {
            // The request failed, we'll keep the Data count?
            number1 = [profileArray count];
        }
    }];

希望对你有用...!