0

Been using FBConnect for selecting friends, but the FBSession's accessTokenData never has the userID of the user that started the session (also isn't present in their login demo). FBFriendPickerViewController returns friend users with their Ids just fine, but I don't see anything about the current user. Is it possible to get the current user's Id from the FB active session, accessTokenData, or some other means?

EDIT: Is it possible to get the user's Id returned from this call?

[FBSession openActiveSessionWithReadPermissions:@[@"public_profile",  @"user_friends"]

It'll be much nicer to do it all in one step.

2 Answers 2

0

You have to request to "/me" to get an 'user_id' since access_token property is encrypted and you couldn't access it from that way.

1
  • Thanks, realizing i'll need to chain completion handlers. Not sure it's very intuitive.
    – 1192805
    Feb 9, 2015 at 15:18
0

Here's what I ended up doing:

if (!FBSession.activeSession.isOpen) {
// if the session is closed, then we open it here, and establish a handler for state changes
[FBSession openActiveSessionWithReadPermissions:@[@"public_profile", @"user_friends"]
                                   allowLoginUI:YES
                              completionHandler:^(FBSession *session,
                                                  FBSessionState state,
                                                  NSError *error) {
                                if (error) {
                                  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                                      message:error.localizedDescription
                                                                                     delegate:nil
                                                                            cancelButtonTitle:@"OK"
                                                                            otherButtonTitles:nil];
                                  [alertView show];
                                }
                                else if (session.isOpen) {

                                  if ([session.declinedPermissions containsObject:@"user_friends"]) {
                                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"friends"
                                                                                        message:@"Must allow accessing friends"
                                                                                       delegate:nil
                                                                              cancelButtonTitle:@"OK"
                                                                              otherButtonTitles:nil];
                                    [alertView show];
                                  }
                                  else {
                                    [[FBRequest requestForMe] startWithCompletionHandler:
                                     ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {

                                       if (error) {
                                         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                                             message:error.localizedDescription
                                                                                            delegate:nil
                                                                                   cancelButtonTitle:@"OK"
                                                                                   otherButtonTitles:nil];
                                         [alertView show];
                                       }
                                       else {
                                         NSLog(@"name %@  id %@", user.first_name, user.objectID);

                                         [[FBRequest requestForMyFriends] startWithCompletionHandler: ^(FBRequestConnection *connection,
                                                                                       NSDictionary* result,
                                                                                       NSError *error) {
                                           NSArray* friends = [result objectForKey:@"data"];
                                           NSLog(@"Found: %i friends", friends.count);

                                           for (NSDictionary<FBGraphUser>* friend in friends) {
                                             NSLog(@"I have a friend named %@ with id %@", friend.name, friend.objectID);
                                           }
                                         }];
                                       }
                                     }];
                                  }
                                }
                              }];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.