3

我已经FBFriendPickerDelegate在我的 .h 文件中实现并使用以下委托来检索信息:

-(BOOL)friendPickerController:(FBFriendPickerViewController *)friendPicker
shouldIncludeUser:(id <FBGraphUser>)user
{
NSLog(@"Birthday:%@",user.birthday);
return YES;
} 

但是每次在 NSLog 上我都会得到一个空值......结果如下。我进入 NSLog :

Birthday:(null) 2012-09-28 10:20:22.450
MyFacebookApp[455:207] Birthday:(null) 2012-09-28 10:20:22.451
MyFacebookApp[455:207] Birthday:(null) 2012-09-28 10:20:22.452
MyFacebookApp[455:207] Birthday:(null) 2012-09-28 10:20:22.454
MyFacebookApp[455:207] Birthday:(null)

请告诉我是否做错了什么。

4

5 回答 5

3

您是否首先要求正确的权限

您将需要user_birthdayfriends_birthday访问用户的响应。他们朋友的生日,和user_location/friends_location以获取他们当前的位置。

尽管当前用户可能会授予您这些朋友的权限,但这并不一定意味着您将获得他们所有朋友的请求数据——是否允许应用程序代表朋友访问该信息取决于这些用户的个人设置。

特别是对于生日字段,您可能只会得到日和月,而不是年份。或者当然,就像任何其他信息一样,什么都没有。

于 2012-09-28T13:57:07.107 回答
2

终于得到答案了......生日可以通过使用Graph API检索

https://graph.facebook.com/me/friends?fields=id,name,birthday&access_token=your_access_token

另请参阅此文档 以获取更多信息。

于 2012-10-12T07:36:29.247 回答
0

这是您在一个快乐的地方需要的所有代码。我必须阅读大量的帖子才能做到这一点。它是直接复制和粘贴代码。如果有帮助,请给它评分。

在您的 *.m 文件中

#import <FacebookSDK/FacebookSDK.h>

/******************
 FACEBOOK
 ******************/

- (IBAction) facebookActionBtn
{

    // Open session with public_profile (required) and user_birthday read permissions
    [FBSession openActiveSessionWithReadPermissions:@[@"public_profile", @"user_friends"]
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session, FBSessionState state, NSError *error) {
         __block NSString *alertText;
         __block NSString *alertTitle;
         if (!error){
             // If the session was opened successfully
             if (state == FBSessionStateOpen)
             {
                 // Your code here
                 NSLog(@"all good ..");

                 [self getUserFriendsBDays];

             }
             else
             {
                 // There was an error, handle it
                 if ([FBErrorUtility shouldNotifyUserForError:error] == YES)
                 {

                     alertTitle = @"Something went wrong";
                     alertText = [FBErrorUtility userMessageForError:error];
                     [[[UIAlertView alloc] initWithTitle:alertTitle
                                                 message:alertText
                                                delegate:self
                                       cancelButtonTitle:@"OK!"
                                       otherButtonTitles:nil] show];

                 } else {
                     // If the user cancelled login
                     if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled)
                     {
                         alertTitle = @"Login cancelled";
                         alertText = @"Your friends birthday will not be entered in our calendar because you didn't grant the permission.";
                         [[[UIAlertView alloc] initWithTitle:alertTitle
                                                     message:alertText
                                                    delegate:self
                                           cancelButtonTitle:@"OK!"
                                           otherButtonTitles:nil] show];

                     } else {
                         NSLog(@"error 2 ..");
                     }
                 }
             }
         }
     }];

}

- (IBAction) getUserFriendsBDays
{

    NSLog(@"promptUserForFriendsBDays ...");




    NSString *fbAccessToken = [[[FBSession activeSession] accessTokenData] accessToken];
    //NSLog(@"permissions::%@ ... fbAccessToken: %@ ...",FBSession.activeSession.permissions, fbAccessToken);


    NSString *request = [NSString stringWithFormat:@"https://graph.facebook.com/me/friends?fields=id,name,birthday&access_token=%@",fbAccessToken];
    NSURL *URL = [NSURL URLWithString:request];

    NSError *error;
    NSString *FBOutPutStr = [NSString stringWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:&error];
    //NSLog(@"FBOutPutStr: %@ ...", FBOutPutStr);


    // Convert to JSON object:
    NSDictionary* jsonObject = [NSJSONSerialization JSONObjectWithData:[FBOutPutStr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL];
    //NSLog(@"jsonObject=%@ ...", jsonObject);

    NSArray* dataField = [[NSArray alloc] initWithArray:[jsonObject objectForKey:@"data"]];
    NSLog(@"dataField=%@ ...", dataField);


    // Extract values:
    NSArray *userIDArray = [dataField valueForKey:@"id"];
    NSArray *userNameArray = [dataField valueForKey:@"name"];
    NSArray *userBdayArray = [dataField valueForKey:@"birthday"];

    NSLog(@"userIDArray=%d ... userNameArray=%d ... userBdayArray=%d ...", [userIDArray count],[userNameArray count],[userBdayArray count]);

}
于 2014-07-09T20:11:07.777 回答
0

在将好友添加到好友列表之前,您需要查询好友列表中的每个用户。

默认情况下,好友列表中的 FBGraphUser 对象仅包含 2 个字段:ID 和名称。更多信息可以通过查询每个好友的用户id获得。

使用 Graph API 的示例:

    [FBRequestConnection startWithGraphPath:[friend id] 
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
// result is your FBGraphUser object for the queried friend
}

更新:我忽略了这一点:有一种方法可以使用查询参数字段来获取所有朋友及其生日。您可以在此处指定所需的字段。(如果你有权限)

http://graph.facebook.com/me/friends?fields=id,name,birthday

于 2013-06-24T15:19:38.210 回答
0

我想我们无法获取朋友的用户名、生日和位置,但我们可以获得登录用户名、生日和所有数据。如果您找到任何解决方案,请告诉我。

于 2012-09-28T06:45:19.800 回答