繁体   English   中英

使用Facebook iOS SDK检索生日

[英]Using Facebook iOS SDK to retrieve birthdays

我是使用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,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)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添加到此文件的菜单上,不要忘记释放它。

我已经对其进行了测试,并且效果很好,希望这会有所帮助。

您可以将NSObject子类化,将其称为“ BirthdayLoader”并在那里实现请求。 现在,在您的控制器中,只需创建这些加载器对象的实例即可检索生日。 当他们成功检索生日时,可以使用委托方法进行报告。

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

考虑到要获得生日(以及其他一些属性,例如用户简历),应用必须由Facebook审核。 否则,尽管正确地授予了权限,但是Graph API调用将不会检索该属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM