我刚开始使用API,所以我不确定如何正确执行它。
我正在努力获得用户朋友的所有生日。到目前为止,我成功获得了他们的朋友列表,然后理论上我可以简单地ping每个ID并从中获取生日。
但是我不确定如何实施这些方法。
当我的viewController加载时:[facebook requestWithGraphPath:@"me/friends" andDelegate:self];
这会发送请求,我实现FBRequestDelegate方法来接收它:
-(void)request:(FBRequest *)request didLoad:(id)result{
NSLog(@"result is : %@", result);
}
到目前为止工作完美,我得到一个包含所有朋友姓名和ID的对象。但是,现在我想遍历每个ID,并发送另外几百个请求。我已经知道如何设置循环和请求调用,但是我已经在这个viewController中使用了didLoad方法,显然我需要在返回数据对象后以不同的方式处理数据。
与(FBRequest *)有关?那是什么,也许我可以去if(request == something)?感谢
答案 0 :(得分:8)
您可以使用单个fql请求检索您(或用户)的facebook好友的生日。
您可以在此处阅读有关fql的信息:http://developers.facebook.com/docs/reference/fql/
但是这里有一些代码供参考(这段代码在一个定义为FBSessionDelegate和FBRequestDelegate的类中)
- (void)request:(FBRequest *)request didLoad:(id)result {
// result is a NSDictionary of your friends and their birthdays!
}
- (void)fbDidLogin {
//some best practice boiler plate code for storing important stuff from fb
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
//now load all my friend's birthdays
NSMutableDictionary * params =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"select birthday, name, uid, pic_square from user where uid in (select uid2 from friend where uid1=me()) order by name",
@"query",
nil];
[self.facebook requestWithMethodName: @"fql.query" andParams: params andHttpMethod: @"POST" andDelegate: self];
}
- (void) loginWithFacebook {
self.facebook = [[[Facebook alloc] initWithAppId:@"<your app id here>" andDelegate:self] autorelease];
//IMPORTANT - you need to ask for permission for friend's birthdays
[facebook authorize:[NSArray arrayWithObject:@"friends_birthday"]];
}
答案 1 :(得分:2)
你应该使用这个代码(我将它与Hackbook代码示例一起使用,它完美地运行):
在APICallsViewController.m上添加以下功能:
/*
/* Graph API: Method to get the user's friends.
*/
- (void)apiGraphFriendsWithBirthdays {
[self showActivityIndicator];
HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"picture,id,name,link,birthday,gender,last_name,first_name",@"fields",
nil];
[[delegate facebook] requestWithGraphPath:@"me/friends" andParams:params andHttpMethod:@"GET" andDelegate:self];
}
上面会获取大量数据,你只能使用id,name和birthday ......
/*
* Helper method to first get the user's friends and birthdays then
* do something with it.
*/
- (void)getFriendsForSetBirthday {
// Call the friends Birthday API first
currentAPICall = kAPIFriendsForSetBirthday;
[self apiGraphFriendsWithBirthdays];
}
将此添加到“ - (void)请求:( FBRequest *)请求didLoad:(id)result”on cases section:
case kAPIFriendsForSetBirthday:
{
NSMutableArray *friends = [[NSMutableArray alloc] initWithCapacity:1];
NSArray *resultData = [result objectForKey:@"data"];
if ([resultData count] > 0) {
for (NSUInteger i=0; i<[resultData count] && i < 25; i++) {
[friends addObject:[resultData objectAtIndex:i]];
NSDictionary *friend = [resultData objectAtIndex:i];
long long fbid = [[friend objectForKey:@"id"]longLongValue];
NSString *name = [friend objectForKey:@"name"];
NSString *birthday = [friend objectForKey:@"birthday"];
NSLog(@"id: %lld - Name: %@ - Birthday: %@", fbid, name,birthday);
}
} else {
[self showMessage:@"You have no friends."];
}
[friends release];
break;
}
你需要申请生日读物的权限(我在喜欢的权限部分做过,但你可以在任何你喜欢的地方做到这一点,注意你必须在它可以工作之前申请):
- (void)apiPromptExtendedPermissions {
currentAPICall = kDialogPermissionsExtended;
HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *extendedPermissions = [[NSArray alloc] initWithObjects:@"user_likes",@"friends_birthday", nil];
[[delegate facebook] authorize:extendedPermissions];
[extendedPermissions release];
}
在.h文件中,不要忘记为apicall添加kAPIFriendsForSetBirthday。
在Dataset.m上添加:
NSDictionary *graphMenu7 = [[NSDictionary alloc] initWithObjectsAndKeys:
@"Get friends birthday", @"title",
@"You can get all friends birthdays.", @"description",
@"Get friends birthday", @"button",
@"getFriendsForSetBirthday", @"method",
nil];
并将graphMenu7添加到此文件的菜单中,不要忘记将其释放。
我测试了它,它的工作完美, 希望这会有所帮助。
答案 2 :(得分:0)
你可以继承NSObject
,称之为“BirthdayLoader”并在那里实现请求。现在,在您的控制器中,只需创建这些装载程序对象的实例即可检索生日。这些可以在成功检索生日时使用委托方法进行报告。
@protocol BirthDayLoaderDelegate
-(void)didFinishLoadingRequest:(FBRequest *)request withResult:(id)result;
@end
答案 3 :(得分:0)
考虑到要获得生日(以及其他一些属性,如用户生物),Facebook必须审核该应用。否则,虽然权限已正确,但Graph API调用不会检索此属性。