12

Is there already a way with the facebook ios API to get a list of friends who have your app installed? I basically require something similar to "Words with Friends" where they can determine which of your FB friends are playing the game. I have one solution which requires some extra database use but was hoping maybe the FB api had something built in for this kind of query.

Firefly
  • 282
  • 5
  • 13
  • 1
    Related: [How to use FQL in the `iOS SDK`](http://stackoverflow.com/questions/4941430/fql-and-graph-api-in-ios) –  Jan 16 '12 at 02:56

4 Answers4

10

Create a FBFriendPickerDelegate delegate class and override this function:

-(BOOL)friendPickerViewController:(FBFriendPickerViewController *)friendPicker shouldIncludeUser:(id<FBGraphUser>)user{
    BOOL installed = [user objectForKey:@"installed"] != nil;
    return installed;
}

For users that do not have your app installed, installed field will be nil, but you have to request it when you load data:

self.friendsController = [[FBFriendPickerViewController alloc] initWithNibName:nil bundle:nil];
//...
NSSet *fields = [NSSet setWithObjects:@"installed", nil];
self.friendPickerController.fieldsForRequest = fields;
//...
[self.friendsController loadData];
pckill
  • 3,709
  • 36
  • 48
  • How can i test it? i want to test it on simulator. But right now none of my friend in my fb friend list having app as its in development right now. I am getting empty list (refrence: friend picker controller sample) . How can i create a dummy user (fb friend ) which can be displayed in app users list. – YogiAR Oct 11 '12 at 07:31
  • Go to facebook and register a new user, add him to your friends and install app on his account. I thought it should be obvious. – pckill Oct 11 '12 at 11:10
  • yes i got it .. Thanks for response .. facebook automatically stores reference when a user logs in to facebook using an ios app .. – YogiAR Oct 11 '12 at 11:47
9

I think fql is a little complex for me,so I use this function in FBRequest to call the Graph API to solve this problem in Facebook SDK 3.5

+ (FBRequest *)requestWithGraphPath:(NSString*)graphPath parameters:(NSDictionary*)parameters HTTPMethod:(NSString*)HTTPMethod;

And the code is:

FBRequest *request =  [FBRequest  requestWithGraphPath:@"me/friends"
                         parameters:@{@"fields":@"name,installed,first_name"}
                         HTTPMethod:@"GET"];

[request startWithCompletionHandler:^(FBRequestConnection *connection,
                                     id result,
                                      NSError *error){



}];

You can put all your interesting fields in the parameters dictionary. And according to the SDK Reference,object containing type (this is always "user"), id (the ID of the user), and optional installed field (always true if returned); The installed field is only returned if the user has installed the application, otherwise it is not part of the returned object

DeXter
  • 179
  • 2
  • 2
2

I've used this code in the past to get app using friends from a user.

facebook is the Facebook class that comes with FB Connect.

NSString *fql = [NSString stringWithFormat:@"SELECT name,uid, pic_small FROM user WHERE is_app_user = 1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = %@) order by concat(first_name,last_name) asc",userId,userId]];

NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:fql ,@"query", [facebook accessToken], @"access_token", nil];

[facebook requestWithMethodName:@"fql.query" andParams:params andHttpMethod:@"GET" andDelegate:self];
Ignacio Inglese
  • 2,605
  • 16
  • 18
  • That looks pretty elegant but I'm not familiar with FQL. How do you determine that it is your app they are running? I thought there would need be an app id needed? or that what is_app_user is? – Firefly Jan 16 '12 at 03:51
  • If you're using Facebook Connect for iOS you should have set your app id when you instantiated it. facebook = [[Facebook alloc] initWithAppId:yourAppId];. After that all your requests are identified with your appid. is_app_user is a flag that says if a certain user has installed the app that sent the request. – Ignacio Inglese Jan 16 '12 at 03:58
  • Ah awesome. Thanks a lot for clarifying! – Firefly Jan 16 '12 at 04:07
  • Have you tried this solution? If it worked, don't forget to green check mark it. – zakdances Jul 10 '12 at 22:22
  • But how would you know that your friend is using a particular app? – Abhinandan Sahgal Oct 22 '12 at 07:01
  • You can only know if the friend is using the app from which you started the request as far as I know. – Ignacio Inglese Oct 22 '12 at 15:18
1

The easiest solution is using the

- (BOOL)friendPickerViewController: shouldIncludeUser: 

method implemented by the FBFriendPickerDelegate protocol in the latest 3.5.1 Facebook SDK (June 2013), asking the delegate whether to include a friend in the list, based on the "installed" field when fetching me/friends.

The installed field is present (true) if the friend is also a user of the calling application.

You can use like this:

- (BOOL)friendPickerViewController:(FBFriendPickerViewController *)friendPicker
                 shouldIncludeUser:(id <FBGraphUser>)user {
        return [[user objectForKey:@"installed"] boolValue];     
}

IMPORTANT! You have to include the followings to include the field in the request and to inform friendPicker about its delegate:

    self.fieldsForRequest = [NSSet setWithObject:@"installed"];
    self.delegate = self;

You can verify with the Graph Api Explorer that only friends with the apps installed display by using the Graph API Explorer and entering the query

me/friends?fields=installed

and you can get back a list of your friends like this, where one of the user has installed your application (true):

...
{
  "id": "100002656xxxxxx"
}, 
{
  "installed": true, 
  "id": "100004366xxxxxx"
},
...
BootMaker
  • 1,639
  • 1
  • 22
  • 24