jsonfacebook-graph-apipaginationios7

Access paging in iOS - previous and next : retrieved in JSON from facebook graph-api


Access paging in iOS Code from JSON previous and next link

In JSON object retrieved from graph api, due to loads of information the information is paged, when I try to access the previous and next link it gives OAuth error. Can anyone help in how to access the link through objective C in iOS app.

I want to know two things - how to retrive 'N' items using --> limit=N and how to open a FBSDKgraphrequest and using the paged link link (containing the paging information)

    paging =         {
        next = "https://graph.facebook.com/v2.3/897401690298128/inbox?access_token=CAAEsxMhiSe4BACWnj27BT6ZBvj2BAxNZCtCNQyCKQORXyylXXkQy3DLSF75UGSz2FydAkQx6Pj49MOS0Q3SGiU1vkQ1iUEs2fQvvlwW3Wc04DEnXZB4CZCza7tOJfyncIPrkFrudQCeRhWUUREqMpCI8Dnm6Ozc6xmwOlT1uN2ZCgQ91llcVC1kV04fiZCqO6H6edFe2YZAUZBy86pw1p4SWCUvgMshzkvZBGgpG8UWG50ZCShdeQPUc86fsuQGOcAno0ZD&limit=25&until=1428241306&__paging_token=enc_AdC9127ZCBVnZACHUlMZBTC39ZC8bSP4ZA8uwQZBdy8xhsZAyKAcxxNdqn48Er3CrVM4DkJPATHhOYBVRm8FuCvYZBU8KSpZA";
        previous = "https://graph.facebook.com/v2.3/897401690298128/inbox?access_token=CAAEsxMhiSe4BACWnj27BT6ZBvj2BAxNZCtCNQyCKQORXyylXXkQy3DLSF75UGSz2FydAkQx6Pj49MOS0Q3SGiU1vkQ1iUEs2fQvvlwW3Wc04DEnXZB4CZCza7tOJfyncIPrkFrudQCeRhWUUREqMpCI8Dnm6Ozc6xmwOlT1uN2ZCgQ91llcVC1kV04fiZCqO6H6edFe2YZAUZBy86pw1p4SWCUvgMshzkvZBGgpG8UWG50ZCShdeQPUc86fsuQGOcAno0ZD&limit=25&since=1432299972&__paging_token=enc_AdDp9ZCK2ZBP40AgTi4TCzaB0QFT1Cy7s1R7HLLDDaT7nbnLYDZB4LZBjiONOqG5QR9Q22KY1oU1LzNOwS5uNZBG7uLF4&__previous=1";
    };

Solution

  • Page information has the current access token, and also depending up different limits restriction paging values will always change , so; its best to use the url and fetch the result. Because the result of next will also have pointer to both previous and next , best method to parse the result obtained is by calling it through a recursive function and passing the paging next values outside and inside the calling function to traverse all pages. Don't forget to put a restriction where you want to stop traversing other nodes. eg:

            NSURL *url = [NSURL URLWithString:PagingNext];
            NSURLRequest *request = [NSURLRequest requestWithURL:url];
            [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *errorConnection)
             {
                 if(errorConnection == nil)
                 {
                     NSError *error;
                     id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
                     if (result == nil)
                     {
                         NSLog(@"Error parsing JSON:\n%@",error.userInfo);
                         return;
                     }
                     //if no error then extract paging next and do what you want -> 
                     /*
                        result = {
                                    data[20],
                                    paging={
                                           previous= "URL_of_previous"
                                           next = "URL_of_next"
                                           }
                                 }
                     */
                     if ([result isKindOfClass:[NSDictionary class]])
                     {
                         NSArray * data = [result objectForKey:@"data"];
                         NSDictionary *paging =[result objectForKey:@"paging"];
                         NSString * resultPagingNext = [paging objectForKey:@"next"];
    
                         [self call_method_recursively_with_parameter_of_next_page:data pagingNext:resultPagingNext];
                     }
                 }
                 else
                 {
                     NSLog(@"Connection Error:\n%@", errorConnection.userInfo);
                     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Network Issue"
                                                                                              message:@"Check if you are connected to Internet" preferredStyle:UIAlertControllerStyleAlert];
                     UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
                                                {
                                                    NSLog(@"User has pressed OK."); }];
                     [alertController addAction:okAction];
                     [self presentViewController:alertController animated:YES completion:nil];
                 }
             }];