简体   繁体   中英

How to retrieve total number of friends in using Facebook graph API

I need just to get total number of Friends for the user using Facebook graph API graph API for iOS.

I saw example which show how to retrieve list of users(ex names). However, in my case, I don't need to get Friend List, I just need to get the total number of Friends. Thus, I think it will be inefficient to get the whole list just to count the total.

Any hint how I could get the total count of friends using acebook graph API graph API for iOS.

I found the answer, just use FBL query : SELECT friend_count FROM user WHERE uid = me()

so the full code will be :

- (void)queryButtonAction {

    NSString *query =@"SELECT friend_count FROM user WHERE uid = me()";

    // Set up the query parameter
    NSDictionary *queryParam =
    [NSDictionary dictionaryWithObjectsAndKeys:query, @"q", nil];
    // Make the API request that uses FQL
    [FBRequestConnection startWithGraphPath:@"/fql"
                                 parameters:queryParam
                                 HTTPMethod:@"GET"
                          completionHandler:^(FBRequestConnection *connection,
                                              id result,
                                              NSError *error) {
                              if (error) {
                                  NSLog(@"Error: %@", [error localizedDescription]);
                              } else {
                                  NSLog(@"Result: %@", result);
                                  // show result
                                  NSArray *friendInfo = (NSArray *) [result objectForKey:@"data"];
                                  NSLog(@"result=%@",[friendInfo objectAtIndex:0]);
                                  NSDictionary *friend_count =[friendInfo objectAtIndex:0];
                                  NSString *numberoff= [friend_count objectForKey:@"friend_count"];
                                   NSLog(@"Result2: %@", numberoff);
                              }
                          }];
}

It is important to understand, that the results may differ.

While the FQL query for user_count will give you the actual number of friends (visible on your profile) counting the friends in graph API will only show friends who did not disable the app platform completely (in privacy settings/apps) eg for my friends list: 303 "real friends" and only 294 if I count the graph api results!

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