Ios Objective-c facebook图形api分页
我正在开发一个iOS应用程序,其中包括一个用户墙的facebook提要。将图形api与以下URL一起使用:Ios Objective-c facebook图形api分页,ios,facebook,Ios,Facebook,我正在开发一个iOS应用程序,其中包括一个用户墙的facebook提要。将图形api与以下URL一起使用: feedURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/feed? access_token=%@&since=%@&until=%@",kFaceBookID,FBSession.activeSession.accessToken, [dateRange objectForKey:@"sin
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剪切并粘贴到web浏览器中,我会得到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);
}
}
}
-(void)连接dFinishLoading:(NSURLConnection*)连接
{
NSString*nextPageURL;
n错误*jsonError;
如果(!jsonError){
NSDictionary*rDict=[NSJSONSerialization JSONObjectWithData:\u postData
选项:0
错误:&jsonError];
nextPageURL=[[rDict objectForKey:@“分页”]objectForKey:@“下一步”];
NSArray*rArray=[rDict objectForKey:@“data”];
DLog(@“Posts Dictionary=%@\n\n”,rDict);
对于(NSDictionary*rPost,以rArray表示){
FBPost*post=[[FBPost alloc]initFBPostWithDictionary:rPost];
[feedsArray addObject:post];
}
}
其他的
{
ALog(@“json错误=%@,[jsonError本地化描述]);
[活动停止动画制作];
NSString*errorMessage=[NSString stringWithFormat:@“Facebook状态请求失败,错误为:%@\n请检查您的网络连接,然后重试”,[jsonError localizedDescription];
[自我退出:错误消息];
}
[feedsTable重新加载数据];
如果(nextPageURL&&[feedsArray计数]<30){
DLog(@“下一个提要URL=%@”,下一个提要URL);
NSURLRequest*request=[NSURLRequest requestWithURL:[NSURL URLWithString:nextPageURL]];
if(![[NSURLConnection alloc]initWithRequest:request委托:self]){
ALog(@“请求连接失败:%@”,请求);
}
}
}
我在回答我自己的问题时,再次审视了整个逻辑,并完全改变了使用[FBRequestConnection…]的方法。如果有人感兴趣,下面是代码。请注意,我一次获取一周的提要消息,以提高tableview性能
- (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];
}
}];
}
-(void)fetchFBFeedsForDateRange:(NSDictionary*)日期范围;
{
_postData=[[NSMutableData alloc]init];
//因为,直到是在时间日期范围内递减一周。
NSDictionary*params=[NSDictionary Dictionary WithObjectsSandKeys:
[dateRange objectForKey:@“自”],@“自”,
[dateRange objectForKey:@“直到”],@“直到”,
零];
NSString*gPath=[NSString stringWithFormat:@“%@/feed”,kFaceBookID];
[FBRequestConnection startWithGraphPath:gPath
参数:params
HTTPMethod:@“获取”
completionHandler:^(FBRequestConnection*连接,id结果,NSError*错误){
如果(!错误){
NSArray*rArray=[result objectForKey:@“data”];
//DLog(@“Posts Array=%@\n\n”,rArray);
对于(NSDictionary*rPost,以rArray表示){
FBPost*post=[[FBPost alloc]initFBPostWithDictionary:rPost];
if(post.type){
如果(!post.skip){
[feedsArray addObject:post];
}
}
}
[feedsTable重新加载数据];
if([feedsArray计数]
能否显示访问下一个URL和获取结果的代码?在原始问题中添加了代码。请注意,如果(!jsonError)…`是,则JSON的反序列化需要在之外进行。我也看到了。