Reputation: 1027
I am using Parse as my database. I want to access the _Users class to see if my Facebook friends are using the app (in the class _User). I have just read this and found out that you can't save friendlist anymore.
The way I did it so far is:
I first get the FB ids of my friends and query it in _User. However, I noticed that the FacebookIDs are saved as authData. How can I query my _User class using FacebookIDs besides creating a new column duplicating facebookIDs as strings?
// Send request to Facebook
[[FBRequest requestForMe] startWithCompletionHandler: ^(FBRequestConnection *connection, id result, NSError *error) {
[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"]];
}
self.fbfriendList = [NSMutableArray arrayWithArray:friendObjects];
PFQuery *friendQuery = [ParseUser query];
//PROBLEM is here where the fbId in my _User class is saved as authData
[friendQuery whereKey:@"fbID" containedIn:friendIds];
NSArray *friendUsers = [friendQuery findObjects];
self.fbfriendList = friendObjects;
}
}];
}];
Upvotes: 0
Views: 771
Reputation: 1141
Parse Users' Facebook IDs are private by default, but you can make them accessible from other users.
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.
When you do your Login with Facebook, you should do it 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];
NSArray *friendUsers = [friendQuery findObjects];
}
}];
Best regards,
Upvotes: 2