Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Facebook friends into UITableView

I am working on integrating Facebook into my iOS app and am wondering how to load Facebook friends into a table view so that the user can select friends to invite to the game, similar to how it is done in Words With Friends.

I know that [facebook requestWithGraphPath:@"me/friends" andDelegate:self]; requests friends from Facebook, but I cannot determine what to do from there. Somehow I want to get the friend ids so that I can send them a message. Any info or advice would be much appreciated. Thanks in advance

like image 618
Jonathan Brown Avatar asked Aug 06 '11 07:08

Jonathan Brown


2 Answers

 - (void)getFriendsResponse {
    FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:@"me/friends" withGetVars:nil];// me/feed 
//parse our json
   SBJSON *parser = [[SBJSON alloc] init];
   NSDictionary *   facebook_response = [parser objectWithString:fb_graph_response.htmlResponse error:nil]; 
   //init array 
        NSMutableArray * feed = (NSMutableArray *) [facebook_response objectForKey:@"data"];
        NSMutableArray *recentFriends = [[NSMutableArray alloc] init];

        //adding values to array
        for (NSDictionary *d in feed) {

            NSLog(@"see Dicitonary :%@",d );
            facebook   = [[Facebook alloc]initWithFacebookDictionary:d ];
            [recentFriends addObject:facebook];
            NSLog(@"Postsss :->>>%@",[facebook sender]);

            [facebook release];
        }

        friends = recentFriends;

        [self.tableView reloadData];
     }

and then Show NSArray in TableView

like image 80
iProgrammer Avatar answered Oct 11 '22 03:10

iProgrammer


You can try this if you want to get the user's friend with profile pictures all at once. First:

- (void) requestFriends:(NSNumber *) fbUserId
{ 
    NSLog(@"requesting friends");

    NSString* fql1 = [NSString stringWithFormat:
                      @"select uid2 from friend where uid1 = %@", fbUserId ];
    NSString* fql2 = [NSString stringWithFormat:
                      @"select uid, name, pic_small from user where uid in (select uid2 from #queryID) order by name"];
    NSString* fql = [NSString stringWithFormat:
                     @"{\"queryID\":\"%@\",\"queryUser\":\"%@\"}",fql1,fql2];

    NSLog(fql);

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:fql forKey:@"queries"];

    [_facebook requestWithMethodName:@"fql.multiquery" andParams:params andHttpMethod:@"GET" andDelegate:self];
}

Then when you get the answer, to process it:

-(void) processFriends:(id) result
{
    //NSLog(@"processFriends %@", result);
    NSArray *queryResults = (NSArray*) result;
    NSMutableDictionary *firstQuery = [queryResults objectAtIndex:0];
    NSMutableDictionary *secondQuery = [queryResults objectAtIndex:1];

    NSArray *resultSet1 = [firstQuery objectForKey:@"fql_result_set"];
    NSString *resultName1 = [firstQuery objectForKey:@"name"];

    NSArray *resultSet2 = [secondQuery objectForKey:@"fql_result_set"];
    NSString *resultName2 = [secondQuery objectForKey:@"name"];

    NSLog(@"%@\n\%@", resultName2, resultSet2);

}

Basically the resultSet2 is an Array of entries that each contain a friend, with these keys in the dictionaries for each friend: - uid - name - pic_small

Therefore you have all you need. Hope it helps!

like image 22
mlegris Avatar answered Oct 11 '22 04:10

mlegris