Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FaceBook Messenger SDK How to get the user's name

Tags:

ios

facebook

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?

like image 729
N0mi Avatar asked Aug 16 '15 11:08

N0mi


1 Answers

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 = "[email protected]";
    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)
                    }
                })
like image 186
Adriano Spadoni Avatar answered Sep 30 '22 13:09

Adriano Spadoni