您应该使用此代码(我将其与 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 和生日...
/*
* 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”中:
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添加到该文件的菜单中,不要忘记释放它。
我已经对其进行了测试,并且效果很好,希望对您有所帮助。