简体   繁体   English

使用Facebook iOS SDK检索生日

[英]Using Facebook iOS SDK to retrieve birthdays

I'm new to using API's, so I'm not sure how to properly execute this. 我是使用API​​的新手,所以我不确定如何正确执行此操作。

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. 到目前为止,我已经成功地获取了他们的friendList,然后从理论上讲,我可以简单地ping每个ID并从中获取生日。

However I'm unsure how on to implement the methods. 但是我不确定如何实现这些方法。

When my viewController loads: [facebook requestWithGraphPath:@"me/friends" andDelegate:self]; 当我的viewController加载时: [facebook requestWithGraphPath:@"me/friends" andDelegate:self];

This sends off the request and I implement the FBRequestDelegate method to receive it: 这发出了请求,我实现了FBRequestDelegate方法来接收它:

-(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. 到目前为止,一切正常,我得到了一个包含所有朋友名称和ID的对象。 However, now I'd like to loop through each ID, and send off another few hundred requests. 但是,现在我想遍历每个ID,并发送另外几百个请求。 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. 我已经知道如何设置循环和请求调用,但是我已经在该viewController中使用了didLoad方法,并且很明显,一旦返回数据对象,我显然需要以不同的方式处理数据。

Something to do with a (FBRequest *)? 与(FBRequest *)有关系吗? What is that, maybe I can go something like if(request == something)? 那是什么,也许我可以去做类似if(request == something)的事情? Thanks 谢谢

You can retrieve the birthdays of your (or the user's) facebook friends with a single fql request. 您可以通过单个fql请求来检索您(或用户)的Facebook朋友的生日。

You can read about fql here: http://developers.facebook.com/docs/reference/fql/ 您可以在此处阅读有关fql的信息: 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) 但是,这里有一些代码可供参考(此代码在定义为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"]];
}

You should use this code (I use it with Hackbook code sample and it works perfectly): 您应该使用以下代码(我在Hackbook代码示例中使用了它,并且效果很好):

On APICallsViewController.m add those functions: 在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];

} }

The above would fetch lots of data, You can use only id,name and birthday... 上面将获取大量数据,您只能使用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];
}

add this to "- (void)request:(FBRequest *)request didLoad:(id)result" on cases section : 将此添加到“案例”部分的“-(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;


    }

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. 在.h文件中,请不要忘记为apicall添加kAPIFriendsForSetBirthday。

On Dataset.m add : 在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];

and add graphMenu7 to the menu on this file, and don't forget to release it. 并将graphMenu7添加到此文件的菜单上,不要忘记释放它。

I've tested it and it's work perfectly, Hope this helps. 我已经对其进行了测试,并且效果很好,希望这会有所帮助。

You could subclass an NSObject , call it "BirthdayLoader" and implement the requests there. 您可以将NSObject子类化,将其称为“ BirthdayLoader”并在那里实现请求。 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

Take in account that to get the birthday (and some other attributes like the user bio) the app has to be reviewed by Facebook. 考虑到要获得生日(以及其他一些属性,例如用户简历),应用必须由Facebook审核。 Otherwise, although the permissions were given correctly the Graph API call will not retrieve this attribute. 否则,尽管正确地授予了权限,但是Graph API调用将不会检索该属性。

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

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