FaceBook Messenger SDK How to get the user's name - ios

I am working on messenger app, mostly by looking at the docs here
The only gotcha is how to pass the user information to the person you are sharing the content with?
From docs it seems that FBSDKMessengerURLHandlerReplyContext may contain userID
NSSet *userIds = context.userIDs
Yet, how can i use the userID to get the user information? Or is it even the right way to fetch user info?
It seems like this task is doable. This game does that
It can tell the user info in first screen, without actually asking user to login.
Any Idea or help on how to make it happen?

If the user IDs are from users that already authenticate in your app, it can be achieved using graph API, like they say on facebook Docs:
User IDs can be used with the Facebook SDK and Graph API
(https://developers.facebook.com/docs/graph-api) to query names,
photos, and other data. This will only contain IDs of users that have
also logged into your app via their Facebook account.
Here some code on how to use the graph API with user id. You can make a call as it follows:
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:[NSString stringWithFormat:#"/%#", {userID-goes-here}]
parameters:#{#"fields":#"name,id,picture,gender,birthday,email"}
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if (!error){
NSLog(#"result: %#",result);
}
}];
the result of this request looks like this:
{
birthday = "06/24/1981";
email = "XXXXXX#hotmail.com";
gender = male;
id = 67XXXXX39;
name = "Adriano Spadoni";
picture = {
data = {
"is_silhouette" = 0;
url = "https://fbcdn-prof... ...jpg";
};
};
}
Just remember, some fields maybe be null.
You probably will need to do a loop requesting user by user I'm afraid, if someone knows how to ask a patch of users at the same time, please show us how.
BUT
If you want to get info from users that NOT installed your app yet, you need to:
FBSDKGraphRequest(graphPath: "me/taggable_friends", parameters: ["fields":"installed,id,name,picture.width(1000)"]).startWithCompletionHandler({ (connection, result, err) -> Void in
if err == nil {
print(result)
}
})

You can't get the username anymore because /me/username is no longer available.
Source: https://developers.facebook.com/docs/apps/changelog#v2_0_graph_api
If you want to detect returning users, use the (App Scoped) ID instead.

Related

Get the relationship status of user's Facebook friends

This is what I'm using now:
if ([FBSDKAccessToken currentAccessToken]) {
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/me?fields=id,name,friends{relationship_status}"
parameters:nil
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
NSLog(#"Result: %#", result);
}];
}
but the result contains an empty list of friends. I guess this list contains the friends that have installed this same app.
Using the Graph Tool Explorer I can get this list
GET /v2.3/me?fields=id,name,friends{relationship_status,gender}
How can I get this list in iOS?
Permissions that I'm calling are:
loginButton.readPermissions = #[#"public_profile", #"email", #"user_friends"];
***** UPDATE *****
Ok, I've read the confirmation: "Friend list now only returns friends who also use your app: The list of friends returned via the /me/friends endpoint is now limited to the list of friends that have authorized your app."
Has anyone used taggable_friends or something like that to get the list of friends making more requests later to get the information about each and every friend? Is this something Facebook can reject when submitting the app?
That's not possible, because all friends_* permissions have been removed. Please don't waste your time trying to find a workaround.
See
https://developers.facebook.com/docs/apps/changelog#v2_0
All friends_* permissions have been removed.

Parameter for like count with facebook api V2.3

I am using this line of code for getting count of likes on facebook post
NSString *postId = #"1234567890_1234567890";
NSMutableDictionary* photosParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:postId,#"ObjectId",nil];
NSString * str = (NSString *) [[[FBSDKGraphRequest alloc] initWithGraphPath:#"/{object-id}/likes"
parameters:photosParams
HTTPMethod:#"GET"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if ([error.userInfo[FBSDKGraphRequestErrorGraphErrorCode] isEqual:#200]) {
NSLog(#"%#",[error localizedDescription]);
}
}];
but I am getting error of unsupported url, any one have Idea what parameter I use to get result. I am using updated version of facebook api
As
https://developers.facebook.com/docs/graph-api/reference/v2.3/object/likes
states, you need
The same permissions required to view the parent object are required to view likes on that object.
That means that the permission setting for Posts (https://developers.facebook.com/docs/graph-api/reference/v2.3/post) apply:
For page posts, public posts by the page are retrievable with any valid access token. Posts by people on the page, posts by people which mention the page, or targeted page posts (by language or geography, for example) may require a user or page token.
A user access token with read_stream or user_posts permission for any other posts
Are you sure you're adding an Access Token to your request?
You can request multiple posts at once like this:
/?ids={object_id1},{object_id2}&fields=id,created_time,likes.summary(true).limit(0)

Missing e-mail when using ParseUI with Facebook login/signup

I'm using the the PFLogInViewController of the ParseUI framework. I have two questions related to emails with regards to Facebook Login within the framework:
1) When signing up via Facebook, I see that the email field of my new user is 'undefined.' How can I obtain the user's email address?
2) If I am able to obtain the email address, does parse's application setting of 'emailVerified' still send a verification email to the user upon Facebook signup? If not, how can this be done?
Thanks.
In order to read the user's email address you'll need to add email to your array of Facebook permissions you're requesting. Look at the Parse tutorial on this https://www.parse.com/tutorials/login-and-signup-views#properties
In your case, you'll want to add #"email" like this:
[logInViewController setFacebookPermissions:[NSArray arrayWithObjects:#"friends_about_me", #"email", nil]];
Then when you are requesting the user information from Facebook, you will be able to access the user's email address:
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Now you can access user's email
NSString *email = result[#"email"];
if (email) {
// Save it to your email field in Parse
}
}
}];
Regarding part 2 of your question, I'm not as familiar with the email validation functionality that Parse supports, but looking at this (admittedly old) response https://www.parse.com/questions/email-verification-emails-going-out-to-facebook-users it appears that Parse will validate email addresses even when the user logs in with Facebook.

Getting the user’s Facebook list of friends

After being able to create a small iOS app that logs in to Facebook. I would like this app to get the user’s list of friends.
Though I browsed the internet for a while and tried various approach I did not succeed to get what I wanted.
In the viewDidLoad method I use this code to start with:
loginView = [[FBLoginView alloc] initWithReadPermissions:#[#"user_friends"]];
And then I implement the loginViewFetchedUserInfo: user: method this way:
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user
{
[FBRequestConnection startWithGraphPath:#"/me/friends"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(#“Result: %#",result);
}];
}
When I execute the app I get this result:
Result: {
data = (
);
summary = {
"total_count" = 267;
}; }
But what I really want is a list of the friends with their names …etc…
All the things I tried for that didn’t work. Though I suppose the solution must be simple.
I also used code like this in the method above ….. but with no luck:
NSArray* friends = [result objectForKey:#"data"];
One more point, here https://developers.facebook.com/docs/graph-api/reference/v2.1/user/friends one can read:
This will only return any friends who have used (via Facebook Login)
the app making the request.
But the result I get (267) does not match this statement. It (267) is the number of friends the user has on Facebook, which is in fact what I am interested in.
I hope someone has something to say to put me on the right track and make things clearer.
Since Facebook's 2.x SDK upgrade they have tightened up access to information in the "Open Graph API."
Under Permissions That Do Not Require Review
App friends. This optional permission grants your app the ability to read a list of friends who also use your app.
You get automatic access to any friends that already use your app (per its Facebook ID.)
You will have to request Extended Permissions if you want to access the list of all a user's friends. That means going through Facebook's App Review process which can be quite challenging (and annoying.)
Frankly, I am not even sure that it is possible to use the new Open Graph API to get the user's entire friend list.

Facebook requestForMyFriends returns no friends data [duplicate]

This question already has answers here:
Facebook Graph API v2.0+ - /me/friends returns empty, or only friends who also use my application
(8 answers)
Closed 8 years ago.
I used the FBRequest to request the friend list of user.
FBRequest *requestFriendList = [FBRequest requestForMyFriends];
[requestFriendList startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error){
if (!error)
{
NSArray *fbFriends = [result objectForKey:#"data"];
//...
}
}];
However, the returned result of the request didn't contain any data. I printed out results, it contains the friend count, but no data inside.
Printing description of result:
{
data = (
);
summary = {
"total_count" = 585;
};
}
If the request failed or the app doesn't have permissions, should it return an error or the "total_count" also is not shown?
So is there anything I did wrong? Anyone can help me fix this problem?
Thanks!!
Graph API 2.0 changed the behaviour of the /friends endpoint.
It now returns the list of friends who have authorized your Facebook app, as opposed to the complete list of friends. It's also a new permission - user_friends.
From the Facebook Platform Changelog:
Friend list is no longer part of the default permission set and has
its own permission: Asking for access to a person's friend list is now
a separate permission that your app must request. The new permission
is called user_friends.
Friend list now only returns friends who also
use your app: The list of friends returned via the /me/friends
endpoint is now limited to the list of friends that have authorized
your app.

Resources