使用Facebook iOS SDK检索生日

我是使用API​​的新手,所以我不确定如何正确执行此操作。

我正试图获得用户朋友的所有生日。 到目前为止,我成功地获得了他们的friendList,然后理论上我可以简单地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)那样呢? 谢谢

您可以使用一个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"]]; } 

你应该使用这个代码(我使用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,姓名和生日…

 /* * 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)request:(FBRequest *)request 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到这个文件的菜单,不要忘记释放它。

我已经testing过它,它的工作完美,希望这会有所帮助。

你可以inheritance一个NSObject ,称之为“BirthdayLoader”,并在那里实现请求。 现在,在您的控制器中,只需创build这些加载器对象的实例来检索生日。 当他们成功检索了一个生日时,这些可以用一个委托方法报告。

 @protocol BirthDayLoaderDelegate -(void)didFinishLoadingRequest:(FBRequest *)request withResult:(id)result; @end 

考虑到要获得生日(以及其他一些属性,如用户生物)的应用程序必须由Facebook审查。 否则,虽然权限被正确给定,但Graph API调用将不会检索此属性。