Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching user details from Facebook in iOS

I'm new to iPhone development. I'm doing an App where the user has 2 login through Facebook, once he submits his credentials and clicks on Sign In button, I have to fetch details like First name, email and gender from Facebook and after fetching the user has to be directed to the Registration page of the app with the details filled and i need this app to be compatible for iPhone 4,4s and 5.

I tried doing this using the Facebook Graph API but couldn't get it, so anyone can help me out.

Thanks in Advance.

like image 241
user2419998 Avatar asked May 25 '13 04:05

user2419998


People also ask

How do I pull data from Facebook?

If you want to download a copy of your information from Facebook, you can use the Download Your Information tool. Tap in the top right of Facebook. Scroll down and tap Settings. Scroll down to Your Facebook Information and tap Download Your Information.

What data can I get from Facebook login?

Facebook Login also enables you to ask for permissions when people log in to your app. These permissions, if granted by the user, give your app access to items of user data. For example, your app can access a user's name and profile photo.

Is there a Facebook API?

The Graph API is the primary way to get data into and out of the Facebook platform. It's an HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.


2 Answers

You can do this by using following code :

[FBSession openActiveSessionWithReadPermissions:@[@"email",@"user_location",@"user_birthday",@"user_hometown"]
                                   allowLoginUI:YES
                              completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {

                                  switch (state) {
                                      case FBSessionStateOpen:
                                          [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                                              if (error) {
                                               NSLog(@"error:%@",error);                                               
                                              }
                                              else
                                              {                                                   
                                                  // retrive user's details at here as shown below
                                                  NSLog(@"FB user first name:%@",user.first_name);
                                                  NSLog(@"FB user last name:%@",user.last_name);
                                                  NSLog(@"FB user birthday:%@",user.birthday);
                                                  NSLog(@"FB user location:%@",user.location);
                                                  NSLog(@"FB user username:%@",user.username);
                                                  NSLog(@"FB user gender:%@",[user objectForKey:@"gender"]);
                                                  NSLog(@"email id:%@",[user objectForKey:@"email"]);
                                                  NSLog(@"location:%@", [NSString stringWithFormat:@"Location: %@\n\n",
                                                                         user.location[@"name"]]);

                                              }
                                          }];
                                          break;

                                      case FBSessionStateClosed:
                                      case FBSessionStateClosedLoginFailed:
                                          [FBSession.activeSession closeAndClearTokenInformation];
                                          break;

                                      default:
                                          break;
                                  }

                              } ];

and don't forgot to import FacebookSDK/FacebookSDK.h in your code.

EDIT : Update for Facebook SDK v4 (23 April,2015)

Now, Faceboook have released new SDK with major changes. In which FBSession class is deprecated. So all users are suggested to migrate to new sdk and APIs.

Below I have mentioned, how we can get user details via Facebook SDK v4 :

if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
  if (!error) {
     NSLog(@”fetched user:%@”, result);
  }
 }];
}

But before fetching user details, we have to integrate new Facebook login in our code as described in Documentation here.

Here is the Changelog for SDK v4. I suggest going through it for being updated.

like image 60
uniruddh Avatar answered Sep 20 '22 18:09

uniruddh


Add info.plist file:

enter image description here

  - (IBAction)btn_fb:(id)sender
    {
      if (!FBSession.activeSession.isOpen)
            {
                NSArray *_fbPermissions =    @[@"email",@"publish_actions",@"public_profile",@"user_hometown",@"user_birthday",@"user_about_me",@"user_friends",@"user_photos",];

        [FBSession openActiveSessionWithReadPermissions:_fbPermissions 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)
             {
                 [self btn_fb:sender];
             }


         }];


        return;
    }


    [FBRequestConnection startWithGraphPath:@"me" parameters:[NSDictionary dictionaryWithObject:@"cover,picture.type(large),id,name,first_name,last_name,gender,birthday,email,location,hometown,bio,photos" forKey:@"fields"] HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
     {
         {
             if (!error)
             {
                 if ([result isKindOfClass:[NSDictionary class]])
                 {
                     NSLog(@"%@",result);

                 }
             }
         }
     }];
}
like image 44
Gami Nilesh Avatar answered Sep 19 '22 18:09

Gami Nilesh