Tom Testicool
Tom Testicool

Reputation: 563

How to get FBFriendPickerViewController to show ALL friends

1)

There has been a change in the FBFriendPickerViewController with the recent version of the Facebook SDK. It used to give you a Friend Picker with all of your Facebook friends but now it only shows friends that have used your app. Handy, but I have functionality in my application that needs to choose from the users entire friends.

Senario = I am trying to use the friend picker to choose a friend and then after selection a segue is performed that shows a view controller that gives the user details of that friend they just selected. That means the selected friends "id" must persist to the next view controller so I can retrieve friends information using the "id" on the next VC (which makes FBFriendPickerViewController handy because of the friendPicker.selection property).

Question = Does anyone know a way I can get a list of the users entire Facebook friends in the friend picker?

2)

Alternative = Another possible solution could be this way by creating a table view and loading the table up with the JSON from Facebook. From there I can load the friend data into the cellForRowAtIndexPath and that all works fine.

NSString *accessToken = [FBSession activeSession].accessTokenData.accessToken;

NSString *surl = @"https://graph.facebook.com/me/friends?access_token=%@&fields=picture,name,id";

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:surl, accessToken]];

NSURLRequest * request = [NSURLRequest requestWithURL:url];

[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *r, NSData *d, NSError *e) {
    if (e==nil) {
        NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:d options:NSJSONReadingAllowFragments error:nil];


friends = [NSMutableArray arrayWithArray:jsonData[@"data"]];

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:                  (NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

idx = indexPath.row;


NSSortDescriptor *sort=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[friends sortUsingDescriptors:[NSArray arrayWithObject:sort]];

f = friends[idx];

// Configure the cell...

//cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_lightgrey.png"]];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"facebook_blue.png"]];
cell.layer.borderColor = [[UIColor blackColor] CGColor];
//cell.layer.borderWidth = 1;
cell.layer.cornerRadius = 10;

cell.imageView.layer.borderColor = [[UIColor whiteColor] CGColor];
cell.imageView.layer.borderWidth = 1;

cell.imageView.image = [UIImage imageNamed:@"fbDefaultPic.png"];
cell.textLabel.text = f[@"name"];
UIImage *theImage = f[@"image"];


if (theImage==nil) {

    cell.imageView.layer.borderColor = [[UIColor whiteColor] CGColor];
    cell.imageView.layer.borderWidth = 1;


NSString *url = f[@"picture"][@"data"][@"url"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    UIImage *img = [UIImage imageWithData:imgData];
    dispatch_async(dispatch_get_main_queue(), ^{
        cell.imageView.image = img;
        //NSMutableDictionary *newFriend = [NSMutableDictionary dictionaryWithDictionary:f];
        //friends [idx] = newFriend;
        //newFriend [@"image"] = img;



    });

});

}

else {

    cell.imageView.image = theImage;
}


return cell;
}

Problem with alternative = I am finding it extremely difficult to persist the "id" of the selected friend using this way. (Would be super helpful if tableView had a .selection property like FBFriendPickerViewController, but it doesn't...).

Question (for alternative) = Can anyone help me with a way that I can create a tableView of Facebook friends and am able to extract the "id" so I can store it in a singleton variable and use it in my other view controller?

-To summarize -

Problem with 1) = Not all friends show up, but can get friend's "id".

Problem with 2) = All friends show up, but can not get friend's "id".

Upvotes: 4

Views: 1327

Answers (1)

Ansen E Anand
Ansen E Anand

Reputation: 107

Facebook Platform Changelog

The most recent version of the API is Version 2.2, which was introduced on October 30, 2014.

Friend list now only returns friends who also use your app: The list of friends returned via the /me/friends endpoint is now limited to the list of friends that have authorized your app.

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

Upvotes: 1

Related Questions