我正在开发一个 iOS 应用程序,其中包含用户墙的 Facebook 提要。使用具有以下 URL 的图形 API:
feedURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/feed?
access_token=%@&since=%@&until=%@",kFaceBookID,FBSession.activeSession.accessToken,
[dateRange objectForKey:@"since"], [dateRange objectForKey:@"until"]];
我返回的数据只有一个结果和一个用于分页的字典条目。当我使用“下一个”URL 执行 NSURLRequest 时,我得到 0 个结果。如果我将相同的 URL 剪切并粘贴到网络浏览器中,我会得到 25 个结果。有什么想法吗?
这是我使用的代码:
- (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);
}
}
}
最佳答案
我正在回答我自己的问题,因为我重新审视了整个逻辑并完全改变了我使用 [FBRequestConnection...] 的方法。如果有人感兴趣,这是代码。请注意,我一次获取一周的提要消息以提高表格 View 的性能。
- (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];
}
}];
}
关于ios - Objective-c facebook graph api分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14006244/