Search code examples
iosobjective-cparse-platformpfquery

Get the datas from the Parse API table and set filters


I m using Parse SDK. I want to get the data from the Parse API table,and want to set the filters as Username and password. How can I achieve this. Below given is the sample code which was done with the help of NSURLSession and wants it to be converted to a code using Parse SDK classes.

-(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];
                   });
          }

Solution

  • Please check Parse Integration here to add parse to your existing project.

    Please try below code:

    // If you are using Custom Table then

     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
         }
     }];
    

    Or You are using USER table then try below code

    [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];
    
            }
        }];