Search code examples
iosfacebookfacebook-ios-sdk

Facebook ios sdk mutual friends


I've been running through the net trying to figure out how to get a list of users that are mutual friends of two users:

For example:

  • User 1: me
  • User 2: Lionel Messi
  • User 2 and User 1 are friends

I need the list of users that friends with both users using the iOS Facebook SDK.


Solution

  • From your edit I think you are looking for this:

    1) Make a query to get the friends list and all the UIDs in that friend lists (let's say you put it into an array self.friendList)

    2) Run a for loop on that list.

    3) Feed this to the FQL query:

    for (NSString * friendID in self.friendList){
        NSString * query = [NSString stringWithFormat:@"SELECT uid1, uid2 FROM friend where uid1= %@ and uid2 in (SELECT uid2 FROM friend where uid1=me())", friendID];
        NSDictionary *queryParam = @{ @"q": query };
        // 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);
              }
           }];
    }
    

    (From: https://developers.facebook.com/docs/ios/run-fql-queries-ios-sdk/)

    4) The result will be the mutual friends between you and that person.

    (This would be if you wanted to get the mutual friends of all your friends, you could obviously just do it with any friendID that you want.)