How can I query Facebook friends in User using Parse? - ios

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;
}
}];
}];

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,

Related

Parse Facebook Login not returning all fields (it returns only id and name) [duplicate]

This question already has answers here:
Facebook JS SDK's FB.api('/me') method doesn't return the fields I expect in Graph API v2.4+
(4 answers)
Closed 7 years ago.
I am using parse framework to manage users.
I have an option in my app where the users can login from facebook.
I have linked the FacebookAppID and URL scheme in plist file.
According to parse documentation the recommended code here is below
[PFFacebookUtils logInWithPermissions:#[ #"email",#"user_about_me", #"user_relationships", #"user_birthday", #"user_location"] block:^(PFUser *user, NSError *error)
{
if (user)
{
NSLog(#"User authenticated from facebook.");
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"Facebook id fetched.");
// result is a dictionary with the user's Facebook data
NSDictionary *userData = (NSDictionary *)result;
}
}
else
{
// User not authenticated
}
]];
Now as you can see i have entered proper permissions for email,user_about_me,user_relationships,user_birthday, user_location
But the result dictionary i am getting from facebook only return name and id everytime and for every user.
I also want rest of the information. Any one know why i am not getting all user data?
Its not duplicate
Use this code-
-(void)fectchUserInfo
{
FBRequest *req = [[FBRequest alloc] initWithSession:[FBSession
activeSession] graphPath:#"me?
fields=first_name,last_name,gender,id,email"];
[req startWithCompletionHandler:^(FBRequestConnection *connection, id result,NSError *error) {
if(error) {
NSLog(#"fb error");
[self handleAuthError:error];
return;
}
else{
facebookFirstName = [result objectForKey:#"first_name"];
facebookLastName = [result objectForKey:#"last_name"];
facebookID = [result objectForKey:#"id"];
facebookEmail = [result objectForKey:#"email"];
NSLog(#"fb data:%# %# %# %# ",facebookFirstName,facebookLastName,facebookID,facebookEmail);
facebookPassword = [NSString stringWithFormat:#"%#_%#",facebookID,#"fb"];
NSLog(#"fb password is :%#",facebookPassword);
}
}];
}

Get Facebook Friends returns empty

I am trying to get a list of friends for my app, however this is what the console returns:
{
data = (
);
summary = {
"total_count" = 1221;
};
The total count is right but it doesn't show me any of my friends?
I have created two test users on the facebook developer portal and they have both logged in with my app, and they are friends with me on facebook, shouldn't their data be showing?
Here is my code for requesting the permissions and such, this class is called each time the user logs in.
#implementation UploadUser
NSString *userName;
NSString *userId;
NSString *userGender;
//UIImage *userPicture;
NSString *userPicture;
NSMutableURLRequest *urlRequest;
//NSData *imageData;
//NSData *pictureData;
//NSData *tempData;
//Retrieves users name, gender and id
-(void)getUserInfo {
FBSDKGraphRequest *requestUser = [[FBSDKGraphRequest alloc]initWithGraphPath:#"me" parameters:nil];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:requestUser completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
userName = result[#"name"];
userGender = result[#"gender"];
userId = result[#"id"];
if (connection) {
if (!error) {
[self setUserData];
[self uploadUser];
}
}else if (!connection) {
NSLog(#"Get User %#",error);
}
}];
[connection start];
//Get Friends
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/me/friends"
parameters:nil
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if (connection) {
NSLog(#"Friends = %#",result);
}else if (!connection) {
NSLog(#"Get Friends Error");
}
}];
}
//Saves user data to class
-(void)setUserData {
DataManager *dm = [DataManager sharedInstance];
[dm setObject:userName forKey:#"userName"];
[dm setObject:userGender forKey:#"userGender"];
[dm setObject:userId forKey:#"userId"];
NSLog(#"%#",userId);
NSLog(#"%#",userName);
NSLog(#"%#",userGender);
}
//Uploads user data online
-(void)uploadUser {
PFUser *user = [PFUser currentUser];
user[#"name"] = userName;
user[#"id"] = userId;
user[#"gender"] = userGender;
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
NSLog(#"User Data Uploaded :)");
} else {
//Handle Error
NSLog(#"User Upload Error... %#",error);
}
}];
}
Why are isn't my query returning my friends? Is it because my friends are not authorised to use my app? Is my permissions not being asked properly?
Also you can use Graph API Explorer in order to quickly check if your request works:
https://developers.facebook.com/tools/explorer/
If you want to check you friends you should ask your friend also use this link and get token. In this way the facebook system understand that your friend login in Graph API app and you will get the list of friends.
Since v2.0 of the Graph API, you can only get the friends who authorized your App with user_friends too. This is subject of countless Stackoverflow threads already, for example: Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app
Check this one Get Facebook friends

get user email from Facebook Using FBgraphUser In Parse

Im Trying to store the users email address in mt parse database but i cant seem to find the right way of getting this done
my code below
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *result, NSError *error) {
if (error) {
// Handle it
} else {
PFUser *me = [PFUser currentUser];
me[#"facebookId"] = result.objectID;
me[#"facebookName"] = result.name;
me[#"facebookProfile"] = result.link;
[me saveEventually];
}
}];
}
Thanks in advance
Actually, result is an NSDictionary instance, conforming to FBGraphUser protocol. That mean it still can be treated as dictionary, for example you can send -objectForKey: message to it. Actually FBGraphUser protocol only adds a "shortcuts" for some objects, stored in the dictionary. So, by calling:
NSString *mail = result[#"email"];
You will get the email. This is a handy equivalent for:
NSString *mail = [result objectForKey:#"email"];
For all possible keys for user look Graph Api Reference.

Parse - Users trying to login with FB after creating account manual - iOS

I am giving to my users the options to sign up manually or using Facebook(I am using Parse). The problem now is that if a user has signup manually and then try to login with Facebook is creating a second account and leaves the email valun to null(becuase already exists). So what is the solution for this one? Is there any official solution? I tried a workaround:
FBRequest *requestEmail = [FBRequest requestForMe];
[requestEmail startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"Error %#",error);
NSDictionary *userData2 = (NSDictionary *)result;
NSString *email2=userData2[#"email"];
PFQuery *query = [PFUser query];
[query whereKey:#"email" equalTo:email2];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
NSString *emailQue =[object objectForKey:#"email"];
if(error || (emailQue==NULL) || (emailQue==nil)){
NSLog(#"User facebook account does not exist");
}
else{
NSLog(#"User facebook email exist");
NSLog(#"email %#",emailQue);
}
}];
}];
which is failing (No active token available) and I believe this is not the correct way. Even if this work then I cant stop the loginWithPermission to create the new user once canceled.
Any suggestions?
If you know that this user is an existing PFUser you can instead link their Facebook account with their existing account. This is a supported Parse feature.
if (![PFFacebookUtils isLinkedWithUser:user]) {
[PFFacebookUtils linkUser:user permissions:nil block:^(BOOL succeeded, NSError *error) {
if (succeeded) {
NSLog(#"Woohoo, user logged in with Facebook!");
}
}];
}
You can get the current user with
PFUser *currentUser = [PFUser currentUser];
Or you could even convert an Anonymous user to a Facebook user.
https://parse.com/docs/ios_guide#fbusers-link/iOS

Getting friends on facebook's scores from parse

So I finally got my app letting users sign into Facebook and parse. Lets call my app a game even though it isn't and lets say that I upload the newest score someone gets every time they play the game. So what I have done is uploaded the "score" to parse under the user. Now, I want the user to be able to see friends that are on Facebook's score. So, I have the user signed in and now how can I check if their friend is using my "game" and then match their email with the one on parse to get there score? As of now, I can get the friends id's of the user that signed in.
BTW. the user has to sign into facebook and make a parse account for my app
I know I am gong to have to use a query to get stuff from parse, I guess my main question is How do I get the facebook users email?
This is how I am getting their id :
- (void)getFacebookInfo {
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Set user's information
NSString *facebookId = [result objectForKey:#"id"];
NSString *facebookName = [result objectForKey:#"name"];
if (facebookName && facebookName.length != 0) {
[[PFUser currentUser] setObject:facebookName forKey:#"displayName"];
}
if (facebookId && facebookId.length != 0) {
[[PFUser currentUser] setObject:facebookId forKey:#"facebookId"];
}
[[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
// Get user's friend information
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSArray *data = [result objectForKey:#"data"];
NSMutableArray *facebookIds = [[NSMutableArray alloc] initWithCapacity:data.count];
for (NSDictionary *friendData in data) {
[facebookIds addObject:[friendData objectForKey:#"id"]];
}
[[PFUser currentUser] setObject:facebookIds forKey:#"facebookFriends"];
[[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
// We're in!
NSLog(#"%#", facebookName);
[self dismissViewControllerAnimated:YES completion:NULL];
}];
} else {
[self showErrorAlert];
}
}];
}];
} else {
[self showErrorAlert];
}
}];
}
In order to get the email address of the Facebook user within the completion handler of startForMyFriendsWithCompletionHandler use:
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSArray *data = [result objectForKey:#"data"];
for (NSDictionary *friendData in data) {
//This is each friend's email address
NSString *aFriendsEmail = [friendData objectForKey:#"email"];
}
}
}];

Resources