Parse Users' Facebook ids are private (and thus cannot be used in a query) by default, and it is up to you as a developer to decide whether this data should be visible to other users of your application.
To accomplish this, you can use the Facebook API to get the user's Facebook ID, assign it to a field on the Parse User. Then you can construct a query on the User collection to get a list of Parse users whose Facebook IDs are in your user's friend list.
In iOS, your code to make your users query-able by Facebook ID would look like this:
// When your user logs in, immediately get and store its Facebook ID
[PFFacebookUtils logInWithPermissions:permissionsArray
block:^(PFUser *user, NSError *error) {
if (user) {
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Store the current user's Facebook ID on the user
[[PFUser currentUser] setObject:[result objectForKey:@"id"]
forKey:@"fbId"];
[[PFUser currentUser] saveInBackground];
}
}];
}
}];
Then, when you are ready to search for your user's friends, you would issue another request:
// Issue a Facebook Graph API request to get your user's friend list
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// result will contain an array with your user's friends in the "data" key
NSArray *friendObjects = [result objectForKey:@"data"];
NSMutableArray *friendIds = [NSMutableArray arrayWithCapacity:friendObjects.count];
// Create a list of friends' Facebook IDs
for (NSDictionary *friendObject in friendObjects) {
[friendIds addObject:[friendObject objectForKey:@"id"]];
}
// Construct a PFUser query that will find friends whose facebook ids
// are contained in the current user's friend list.
PFQuery *friendQuery = [PFUser query];
[friendQuery whereKey:@"fbId" containedIn:friendIds];
// findObjects will return a list of PFUsers that are friends
// with the current user
NSArray *friendUsers = [friendQuery findObjects];
}
}];
For Android, your code to make your users query-able by Facebook ID would look like this:
ParseFacebookUtils.logIn(this, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException error) {
// When your user logs in, immediately get and store its Facebook ID
if (user != null) {
getFacebookIdInBackground();
}
}
});
private static void getFacebookIdInBackground() {
Request.executeMeRequestAsync(ParseFacebookUtils.getSession(), new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
ParseUser.getCurrentUser().put("fbId", user.getId());
ParseUser.getCurrentUser().saveInBackground();
}
}
});
}
Then, when you are ready to search for your user's friends, you would issue another request:
Request.executeMyFriendsRequestAsync(ParseFacebookUtils.getSession(), new Request.GraphUserListCallback() {
@Override
public void onCompleted(List<GraphUser> users, Response response) {
if (users != null) {
List<String> friendsList = new ArrayList<String>();
for (GraphUser user : users) {
friendsList.add(user.getId());
}
// Construct a ParseUser query that will find friends whose
// facebook IDs are contained in the current user's friend list.
ParseQuery friendQuery = ParseQuery.getUserQuery();
friendQuery.whereContainedIn("fbId", friendsList);
// findObjects will return a list of ParseUsers that are friends with
// the current user
List<ParseObject> friendUsers = friendQuery.find();
}
}
});
No comments:
Post a Comment