简体   繁体   中英

Facebook iOS SDK v4.0, get friends list

Seems like Facebook iOS SDK v4.0 has a lot of changes. I try to get a user's friends list after he login. I think it's something to do with FBSDKGraphRequestConnection but I'm not sure how it can be used.

Also, if I only want to get friends who are using my app, does facebook sdk support that?

Can anyone give some examples? Thanks!

FBSDKGraphRequest *friendsRequest = 
    [[FBSDKGraphRequest alloc] initWithGraphPath:@"me/friends"
                                      parameters:parameters];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:friendsRequest
     completionHandler:^(FBSDKGraphRequestConnection *innerConnection, NSDictionary *result, NSError *error) {
         ...
     }];
// start the actual request
[connection start];

This shows how to handle the response:

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me/friends" parameters:nil]
 startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
{
     if (!error)
     {
        NSArray * friendList = [result objectForKey:@"data"];
        for (NSDictionary * friend in friendList)
        {
            NSLog(@"Friend id: %@", friend[@"id"]);
            NSLog(@"Friend name: %@", friend[@"name"]);             
        }
    }
}];

I used below code to get friend list

self.graphrequest = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me/taggable_friends?limit=100"
                                                 parameters:@{ @"fields":@"id,name,picture.width(100).height(100)"}];
[self.graphrequest startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
    if (!error) {
        self.resultsArray = result[@"data"];
        [self.fbTableView reloadData];
        [self.fbTableView setHidden:NO];
    } else {
        NSLog(@"Picker loading error:%@",error.userInfo[FBSDKErrorLocalizedDescriptionKey]);
    }
}];

You can limit the number of friends using limit keyword.Ex: limit=100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM