简体   繁体   中英

how to retrieve friend's birthday using Facebook SDK in ios?

I have implemented FBFriendPickerDelegate in my .h file and using the following delegate to retrieve information:

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

but every time on NSLog I get a null value... Following is the result. I get in 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)

Please tell if I am doing something wrong.

Have you asked for the right permissions first?

You'll need user_birthday and friends_birthday to access the user's resp. their friend's birthdays, and user_location / friends_location to get their current location.

And although the current user might give you these friends permissions, it does not necessarily mean you will get the requested data for all their friends – whether apps are allowed to access that information on behalf of friends depends on these user's individual settings.

Especially for the birthday field it might be, that you'll get only day and month, but not the year. Or of course, like with any other info, nothing at all.

Finally got the answer....birthday can be retrieved by using Graph API

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

Also refer to this documentation for more information.

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

You will need to query for each user in the friends list before adding them to the friends list.

By default, FBGraphUser objects in the friends list only contain 2 fields: ID and name. More information can be obtained by querying the user id of each friend.

An example using the Graph API:

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

UPDATE : I overlooked this: There is a way to get all friends and their birthdays using a query parameter fields. Here you can specify the fields you need. (if you have the permission)

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

Here is all of the code you need in one happy place. I had to read tons of post to get this far. Its a straight copy and paste code. Rate it up if it helps.

In your *.m file

#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]);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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