Reputation: 6119
I'm new to using API's, so I'm not sure how to properly execute this.
I'm trying to get all the birthdays of a user's friends. So far I am successful in getting their friendList, and then in theory I can simply ping each ID and get the birthday from it.
However I'm unsure how on to implement the methods.
When my viewController loads: [facebook requestWithGraphPath:@"me/friends" andDelegate:self];
This sends off the request and I implement the FBRequestDelegate method to receive it:
-(void)request:(FBRequest *)request didLoad:(id)result{
NSLog(@"result is : %@", result);
}
Works perfectly so far, I get an object with all the friend names and IDs. However, now I'd like to loop through each ID, and send off another few hundred requests. I already know how to setup the loop and the request call, but I've already used the didLoad method in this viewController, and I obviously need to handle the data differently once the data object gets returned.
Something to do with a (FBRequest *)? What is that, maybe I can go something like if(request == something)? Thanks
Upvotes: 1
Views: 6372
Reputation: 1305
Take in account that to get the birthday (and some other attributes like the user bio) the app has to be reviewed by Facebook. Otherwise, although the permissions were given correctly the Graph API call will not retrieve this attribute.
Upvotes: 0
Reputation: 9940
You should use this code (I use it with Hackbook code sample and it works perfectly):
On APICallsViewController.m add those functions:
/*
/* 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];
}
The above would fetch lots of data, You can use only id,name and 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];
}
add this to "- (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;
}
You need to request for permissions for birthday readings (I did it on the likes permission section but you can do it wherever you like, Note that you must request it before it can work):
- (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];
}
On the .h file don't forget to add kAPIFriendsForSetBirthday for apicall.
On Dataset.m add :
NSDictionary *graphMenu7 = [[NSDictionary alloc] initWithObjectsAndKeys:
@"Get friends birthday", @"title",
@"You can get all friends birthdays.", @"description",
@"Get friends birthday", @"button",
@"getFriendsForSetBirthday", @"method",
nil];
and add graphMenu7 to the menu on this file, and don't forget to release it.
I've tested it and it's work perfectly, Hope this helps.
Upvotes: 2
Reputation: 12832
You can retrieve the birthdays of your (or the user's) facebook friends with a single fql request.
You can read about fql here: http://developers.facebook.com/docs/reference/fql/
But here's some code for reference (this code is in a class defined to be a FBSessionDelegate, and 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"]];
}
Upvotes: 8
Reputation: 80265
You could subclass an NSObject
, call it "BirthdayLoader" and implement the requests there. Now, in your controller, just create instances of these loader objects to retrieve the birthdays. These could report back with a delegate method when they successfully retrieved a birthday.
@protocol BirthDayLoaderDelegate
-(void)didFinishLoadingRequest:(FBRequest *)request withResult:(id)result;
@end
Upvotes: 0