Anthony Glyadchenko
Anthony Glyadchenko

Reputation: 3622

What is the most efficient way to get a list of a Facebook user's friends' names in the Facebook iOS SDK?

I want to use the Graph API. And I want to load them into a UITableView. Also loading entire user details is slow... Is there a way that I can make it faster?

One more thing: Is there a way that I can load a page on Facebook in the login window that is provided by the Facebook iOS SDK after the user logs in? Pretty much I want to load a certain fan page with the user's login credentials...

Upvotes: 1

Views: 1632

Answers (2)

Jeff
Jeff

Reputation: 6717

Using Facebook SDK 3.0 you can get a list of friends this way:

FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                  NSDictionary* result,
                                  NSError *error) {
    NSArray* friends = [result objectForKey:@"data"];
    NSLog(@"Found: %i friends", friends.count);
    for (NSDictionary<FBGraphUser>* friend in friends) {
        NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id);
    }
}];

Consider KNMultiItemSelector to manage the pick list for you.

Upvotes: 1

Avi Shukron
Avi Shukron

Reputation: 6118

I added the following method to the Facebook Class to get the user's friends list:

-(BOOL) getFriendsList : (id<FBRequestDelegate>) delegate
{
    if (! [self isSessionValid])
    {
        return NO;
    }
    else
    {
        [self requestWithGraphPath:@"me/friends" andDelegate:delegate];
        return YES;
    }
}

For handling the respond from the server i wrote the following method:

- (void)request:(FBRequest *)request didLoad:(id)result
{
    NSDictionary * data = result;
    NSArray *separatedName;
    Contact * current;
    _contactsFromFacebook = [[NSMutableDictionary alloc] init];

    for (NSDictionary * contact in data) 
    {
        separatedName = [[contact objectForKey:@"name"] componentsSeparatedByString:@" "];
        current = [[Contact alloc] 
            initWithFirstName:[separatedName objectAtIndex:0] 
                  andLastName:[separatedName objectAtIndex:1]];
        if(current.alphabeticKey)
        {
            [self.contactsFromFacebook addObject:current toExsistingArrayWithKey:current.alphabeticKey];
            [self addFriendToImportList:current];
        }
        [current release];
    }
    self.allKeysSorted = [NSMutableArray arrayWithArray:[self.contactsFromFacebook allKeys]];
    [self.tableView reloadData];
 }

Notes:

  1. The class "Contact" is a class that i wrote for holding a data about contacts.
  2. I only care about the first and last name of the friend.
  3. I am holding a dictionary to hold all the contacts from facebook, and al the contacts that are in that dictionary will be displayed in a UITableView
  4. At the end of the method, i'm calling [self.tableView reloadData] to (re)load all the contacts that i just got from facebook. if you will try to call it earlier, like in viewDidLoad, you will just end up with an empty table because there are no contacts yet.

Upvotes: 3

Related Questions