xcode 使用 Facebook iOS SDK 检索生日

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10323932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:06:02  来源:igfitidea点击:

Using Facebook iOS SDK to retrieve birthdays

iphoneiosxcodefacebook-graph-api

提问by user339946

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.

我正在尝试获取用户朋友的所有生日。到目前为止,我成功获取了他们的朋友列表,然后理论上我可以简单地 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. 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.

到目前为止工作完美,我得到了一个包含所有朋友姓名和 ID 的对象。但是,现在我想遍历每个 ID,然后再发送几百个请求。我已经知道如何设置循环和请求调用,但是我已经在这个 viewController 中使用了 didLoad 方法,而且一旦数据对象返回,我显然需要以不同的方式处理数据。

Something to do with a (FBRequest *)? What is that, maybe I can go something like if(request == something)? Thanks

与(FBRequest *)有关吗?那是什么,也许我可以使用 if(request == something) 之类的方法?谢谢

回答by Andy Obusek

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"]];
}

回答by Idan

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 和生日...

/*
* 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.

我已经测试过它并且它完美地工作,希望这会有所帮助。

回答by Mundi

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.

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

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

回答by halbano

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.

考虑到要获得生日(以及其他一些属性,如用户简历),应用程序必须经过 Facebook 。否则,尽管正确授予了权限,但图形 API 调用将不会检索此属性。