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

后端 未结 5 1281
难免孤独
难免孤独 2020-12-30 17:33

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

-(BOOL)friendPickerController:         


        
相关标签:
5条回答
  • 2020-12-30 17:41

    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.

    0 讨论(0)
  • 2020-12-30 17:42

    i think we cant get friends user name,birthday and location,but we can get loggedin user username ,birthday and all the data.if you find any solution then please let me know.

    0 讨论(0)
  • 2020-12-30 17:44

    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.

    0 讨论(0)
  • 2020-12-30 17:44

    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]);
    
    }
    
    0 讨论(0)
  • 2020-12-30 18:03

    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

    0 讨论(0)
提交回复
热议问题