Search code examples
objective-cfacebook-ios-sdk

Getting Facebook user info after login in objective c


I'm trying to get the user name, email and picture from Facebook after login. Login and permissions works fine.

I tried using:

FBSession

But Im getting "user of undeclared FBSession" as I was trying to implement:

if (FBSession.activeSession.isOpen) {

[[FBRequest requestForMe] startWithCompletionHandler:
 ^(FBRequestConnection *connection,
   NSDictionary<FBGraphUser> *user,
   NSError *error) {
     if (!error) {
         NSString *firstName = user.first_name;
         NSString *lastName = user.last_name;
         NSString *facebookId = user.id;
         NSString *email = [user objectForKey:@"email"];
         NSString *imageUrl = [[NSString alloc] initWithFormat: @"http://graph.facebook.com/%@/picture?type=large", facebookId];
     }
 }];}

Found it here: How to get user info from Facebook SDK in iOS?

Any ideas how can I achive this? Or why am I getting undeclared for Facebook elements?


Solution

  • If you are using the new facebook sdk and new graph api you have to use access tokens

    if ([FBSDKAccessToken currentAccessToken]) {
    
    
        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"picture,email,birthday,gender,name"}] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id res, NSError *error)
         {
             if (!error) {
    
    
    
                 FBID = [res objectForKey:@"id"];
                 FBDOB = [res objectForKey:@"birthday"];
                 FBEMAIL = [res objectForKey:@"email"];
                 FBGENDER = [res objectForKey:@"gender"];
                 FBUSERNAME = [res objectForKey:@"name"];
    
    
             }
             else
             {
                 NSLog(@"%@", [error localizedDescription]);
             }}];
    }
    

    check out this link on fetching data from facebook https://developers.facebook.com/docs/ios/graph