Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Friends list from Facebook iOS sdk

I am using iOS sdk v3.18.1, I want to get all my Facebook friends.I can get the friends count but, data is nil.

Here is my code

[FBRequestConnection startWithGraphPath:@"me/friends" parameters:nil HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        NSLog(@"result %@",result);
    }];

Out put

{
    data =     (
    );
    summary =     {
        "total_count" = 840;
    };
}
like image 761
Vineesh TP Avatar asked Dec 25 '22 04:12

Vineesh TP


2 Answers

https://developers.facebook.com/docs/apps/changelog

Since v2.0 you cannot get the full friend list anymore, you only get the friends who authorized your App too.

See my answer in this thread too: how to get a list of all user friends (not only who use the app)?

like image 125
andyrandy Avatar answered Dec 28 '22 10:12

andyrandy


// declare an array in header file which will hold the list of all friends - 
NSMutableArray * m_allFriends;

// alloc and initialize the array only once 
m_allFriends = [[NSMutableArray alloc] init];

With FB SDK 3.0 and API Version above 2.0 you need to call below function (graph api with me/friends)to get list of FB Friends which uses the same app.

// get friends which use the app

-(void) getMineFriends
{
    [FBRequestConnection startWithGraphPath:@"me/friends"
                                 parameters:nil
                                 HTTPMethod:@"GET"
                          completionHandler:^(
                                              FBRequestConnection *connection,
                                              id result,
                                              NSError *error
                                              ) {
                              NSLog(@"me/friends result=%@",result);

                              NSLog(@"me/friends error = %@", error.description);

                              NSArray *friendList = [result objectForKey:@"data"];

                              [m_allFriends addObjectsFromArray: friendList];
                          }];
}

Note : 1) The default limit for the number of friends returned by above query is 25. 2)If the next link comes in result, that means there are some more friends which you will be fetching in next query and so on. 3)Alternatively you can change the limit (reduce the limit, exceed the limit from 25) and pass that in param.

////////////////////////////////////////////////////////////////////////

For non app friends -

// m_invitableFriends - global array which will hold the list of invitable friends

Also to get non app friends you need to use (/me/invitable_friends) as below -

- (void) getAllInvitableFriends
{
    NSMutableArray *tempFriendsList =  [[NSMutableArray alloc] init];
    NSDictionary *limitParam = [NSDictionary dictionaryWithObjectsAndKeys:@"100", @"limit", nil];
    [self getAllInvitableFriendsFromFB:limitParam addInList:tempFriendsList];
}

- (void) getAllInvitableFriendsFromFB:(NSDictionary*)parameters
                            addInList:(NSMutableArray *)tempFriendsList
{
    [FBRequestConnection startWithGraphPath:@"/me/invitable_friends"
                                 parameters:parameters
                                 HTTPMethod:@"GET"
                          completionHandler:^(
                                              FBRequestConnection *connection,
                                              id result,
                                              NSError *error
                                              ) {
                              NSLog(@"error=%@",error);

                              NSLog(@"result=%@",result);

                              NSArray *friendArray = [result objectForKey:@"data"];

                              [tempFriendsList addObjectsFromArray:friendArray];

                              NSDictionary *paging = [result objectForKey:@"paging"];
                              NSString *next = nil;
                              next = [paging objectForKey:@"next"];
                              if(next != nil)
                              {
                                  NSDictionary *cursor = [paging objectForKey:@"cursors"];
                                  NSString *after = [cursor objectForKey:@"after"];
                                  //NSString *before = [cursor objectForKey:@"before"];
                                  NSDictionary *limitParam = [NSDictionary dictionaryWithObjectsAndKeys:
                                                              @"100", @"limit", after, @"after"
                                                              , nil
                                                              ];
                                  [self getAllInvitableFriendsFromFB:limitParam addInList:tempFriendsList];
                              }
                              else
                              {
                                  [self replaceGlobalListWithRecentData:tempFriendsList];
                              }
                          }];
}

- (void) replaceGlobalListWithRecentData:(NSMutableArray *)tempFriendsList
{
    // replace global from received list
    [m_invitableFriends removeAllObjects];
    [m_invitableFriends addObjectsFromArray:tempFriendsList];
    //NSLog(@"friendsList = %d", [m_invitableFriends count]);
    [tempFriendsList release];
}

For Inviting non app friend -

you will get invite tokens with the list of friends returned by me/invitable_friends graph api. You can use these invite tokens with FBWebDialogs to send invite to friends as below

- (void) openFacebookFeedDialogForFriend:(NSString *)userInviteTokens {

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   userInviteTokens, @"to",
                                   nil, @"object_id",
                                   @"send", @"action_type",
                                   actionLinksStr, @"actions",
                                   nil];

    [FBWebDialogs
     presentRequestsDialogModallyWithSession:nil
     message:@"Hi friend, I am playing game. Come and play this awesome game with me."
     title:nil
     parameters:params
     handler:^(
               FBWebDialogResult result,
               NSURL *url,
               NSError *error)
     {
         if (error) {
             // Error launching the dialog or sending the request.
             NSLog(@"Error sending request : %@", error.description);
         }
         else
         {
             if (result == FBWebDialogResultDialogNotCompleted)
             {
                 // User clicked the "x" icon
                 NSLog(@"User canceled request.");
                 NSLog(@"Friend post dialog not complete, error: %@", error.description);
             }
             else
             {
                 NSDictionary *resultParams = [g_mainApp->m_appDelegate parseURLParams:[url query]];

                 if (![resultParams valueForKey:@"request"])
                 {
                     // User clicked the Cancel button
                     NSLog(@"User canceled request.");
                 }
                 else
                 {
                     NSString *requestID = [resultParams valueForKey:@"request"];

                     // here you will get the fb id of the friend you invited,
                     // you can use this id to reward the sender when receiver accepts the request

                     NSLog(@"Feed post ID: %@", requestID);
                     NSLog(@"Friend post dialog complete: %@", url);
                 }
             }
         }
     }];
}
like image 33
Sanjay Mohnani Avatar answered Dec 28 '22 09:12

Sanjay Mohnani