25

In the "old" FB iOS SDK I could receive user information via the following:

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"SELECT uid, name, email, pic FROM user WHERE uid=me()", @"query",
                                   nil];

    JBFacebookManager *fbManager = [JBFacebookManager getInstance];
    fbManager.requestDelegate = self;

    [fbManager.facebook requestWithMethodName:@"fql.query"
                                     andParams:params
                                 andHttpMethod:@"POST"
                                   andDelegate:self];

How can I do this with the new FB iOS SDK 3.0? Do I need to use FBRequest or FBOpenGraphAction or FBGraphObject or a combination of those or something completely different?

remudada
  • 3,751
  • 2
  • 33
  • 60
znq
  • 44,613
  • 41
  • 116
  • 144

5 Answers5

68
if (FBSession.activeSession.isOpen) {
    [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
         if (!error) {
             self.nameLabel.text = user.name;
             self.emailLabel.text = [user objectForKey:@"email"];
         }
     }];
}

Because FBGraphUser doesn't have an email @property, we can't access the information like with the name (dot-syntax), however the NSDictionary still has the email kv-pair and we can access it like we would do with a normal NSDictionary.

Don't forget to ask for the email permission though:

NSArray *permissions = [[NSArray alloc] initWithObjects:@"email", nil];
[FBSession sessionOpenWithPermissions:permissions completionHandler:
 ^(FBSession *session, FBSessionState state, NSError *error) {
     [self facebookSessionStateChanged:session state:state error:error];
 }];
znq
  • 44,613
  • 41
  • 116
  • 144
  • 4
    sessionOpenWithPermissions:@[@"email"]; for a short and more readable line. – Raphael Oliveira Jan 22 '13 at 11:39
  • is there a way like FBLoginView that with one WS you can get the user information? Like this (by the way, this is how I implemented it), if you want to log in to your own system after the facebook log in you have 3 consecutives WS. – Cristian Pena Feb 26 '15 at 17:48
5

Once you have access to (id<FBGraphUser>)user, you could simply use, user[@"email"].

Zorayr
  • 23,770
  • 8
  • 136
  • 129
1

Did you read the source code of the 3.0 SDK? There is a method that I think is identical:

- (FBRequest*)requestWithMethodName:(NSString *)methodName
                          andParams:(NSMutableDictionary *)params
                      andHttpMethod:(NSString *)httpMethod
                        andDelegate:(id <FBRequestDelegate>)delegate;
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
jasondinh
  • 918
  • 7
  • 21
  • Not exactly the same: https://developers.facebook.com/docs/sdk-reference/iossdk/3.0/class/FBRequest/. I figured out a solution and just posted it as well. – znq Aug 07 '12 at 10:50
1

from this we can get facebook user's basic info and email id.

[FBSession openActiveSessionWithReadPermissions:@[@"basic_info",@"email"] allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState status,NSError *error){
        if(error)
        {
            NSLog(@"Facebook Error : %@",error.localizedDescription);
        }
        else{

        }
        // Respond to session state changes,
        // ex: updating the view

    }];
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
0

The easiest method for getting the info after logging in is :

-(IBAction)loginWithFacebook:(id)sender{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
 logInWithReadPermissions: @[@"public_profile",@"email"]
 fromViewController:self
 handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
     if (error) {
         NSLog(@"Process error");
     } else if (result.isCancelled) {
         NSLog(@"Cancelled");
     } else {
         NSLog(@"Logged in");
         [self getFacebookProfileInfos];

     }
  }];
}

-(void)getFacebookProfileInfos {
NSDictionary *parameters = @ {@"fields": @"id, name, first_name, last_name, picture.type(large), email"};
if ([FBSDKAccessToken currentAccessToken]) {
    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
         if (!error) {
             NSLog(@"fetched user:%@", result);
         }
     }];
}

}

You will get all the info the result.

Ashish
  • 1,978
  • 1
  • 15
  • 7