简体   繁体   中英

Objective-c facebook graph api pagination

I'm developing an iOS app that includes a facebook feed of a users wall. Using the graph api with the following URL:

feedURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/feed?
access_token=%@&since=%@&until=%@",kFaceBookID,FBSession.activeSession.accessToken,
[dateRange objectForKey:@"since"], [dateRange objectForKey:@"until"]]; 

I get back data that only one result and a dictionary entry for paging. When I do a NSURLRequest with the "next" URL I get back 0 results. If I cut and paste that same URL into a web browser I get back 25 results. Any ideas on why?

Here is the code I am using:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *nextPageURL;
    NSError *jsonError;
    if (!jsonError) {
        NSDictionary *rDict = [NSJSONSerialization JSONObjectWithData:_postData
                                                              options:0
                                                                error:&jsonError];
        nextPageURL = [[rDict objectForKey:@"paging"]objectForKey:@"next"];
        NSArray *rArray = [rDict objectForKey:@"data"];
        DLog(@"Posts Dictionary = %@\n\n",rDict);
        for (NSDictionary *rPost in rArray) {
            FBPost *post = [[FBPost alloc]initFBPostWithDictionary:rPost];
            [feedsArray addObject:post];
        }
    }
    else
    {
        ALog(@"json error = %@",[jsonError localizedDescription]);
        [activity stopAnimating];
        NSString *errorMessage = [NSString stringWithFormat:@"Facebook status request failed with error: %@\nCheck your network connection and try again later",[jsonError localizedDescription]];
        [self quit:errorMessage];
    }
    [feedsTable reloadData];

    if (nextPageURL && [feedsArray count] < 30) {
        DLog(@"Next Feed URL = %@",nextPageURL);

        NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:nextPageURL]];
        if (![[NSURLConnection alloc] initWithRequest:request delegate:self]) {
            ALog(@"Connection failed for request: %@",request);
        }

    }

}

I am answering my own question as I took another look at the entire logic and completely changed my approach to use [FBRequestConnection...] instead. Here is the code if anyone is interested. Note that I fetch one weeks worth of feed messages at a time to improve the tableview performance.

- (void) fetchFBFeedsForDateRange:(NSDictionary *)dateRange;
{
    _postData = [[NSMutableData alloc]init];
    //since, until is a decremented one week at a time date range. 
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            [dateRange objectForKey:@"since"], @"since",
                            [dateRange objectForKey:@"until"], @"until",
                            nil];
    NSString *gPath = [NSString stringWithFormat:@"%@/feed",kFaceBookID];
    [FBRequestConnection startWithGraphPath:gPath
                                 parameters:params
                                 HTTPMethod:@"GET"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              if (!error) {
                                  NSArray *rArray = [result objectForKey:@"data"];
                                  //DLog(@"Posts Array = %@\n\n",rArray);
                                  for (NSDictionary *rPost in rArray) {
                                      FBPost *post = [[FBPost alloc]initFBPostWithDictionary:rPost];
                                      if (post.type) {
                                          if (!post.skip) {
                                              [feedsArray addObject:post];
                                          }
                                      }
                                  }
                                [feedsTable reloadData];
                                  if ([feedsArray count] <  kFaceBookMaxPostsToDisplay) {
                                      [self performSelector:@selector(fetchPreviousWeek)];
                                  }
                                  else
                                  {
                                      [activity stopAnimating];
                                  }
                              }
                              else
                              {
                                  [activity stopAnimating];
                                  NSString *errorMessage = [NSString stringWithFormat:@"Facebook status request failed with error: %@\nCheck your network connection and try again later",[error localizedDescription]];
                                  [self quit:errorMessage];

                              }

                          }];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM