daspianist daspianist - 1 year ago 73
iOS Question

Viewing and using Facebook login user data

I followed the instructions provided by Facebook to add a "Login with Facebook" button in my app. In order to populate additional view controllers, I will need to pull data from the logged in user such as name and email. However, there does not seem to be a way to access this data.

For reference, the following code is pasted directly from Facebook instructions.

FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;
loginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];
[self.view addSubview:loginButton];

Any assistance is much appreciated. Thanks!

Answer Source

If you have Custom Facebook Button Then Just Call the Below Code On Button action:

I already checked it and it works fine for me. See the reference:

NSMutableDictionary *fbResultData;
fbResultData =[[NSMutableDictionary alloc]init];
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions: @[@"public_profile", @"email", @"user_friends"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
     if (error) {
         NSLog(@"Process error");
     } else if (result.isCancelled) {
     } else {
         NSLog(@"Logged in");
         if ([FBSDKAccessToken currentAccessToken])
             [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me?fields=id,name,age_range,birthday,devices,email,gender,last_name,family,friends,location,picture" parameters:nil]
              startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                  if (!error) {
                      NSString * accessToken = [[FBSDKAccessToken currentAccessToken] tokenString];
                      NSLog(@"fetched user:%@ ,%@", result,accessToken);
                      if ([result objectForKey:@"email"]) {
                          [fbResultData setObject:[result objectForKey:@"email"] forKey:@"email"];
                      if ([result objectForKey:@"gender"]) {
                          [fbResultData setObject:[result objectForKey:@"gender"] forKey:@"gender"];
                      if ([result objectForKey:@"name"]) {
                          NSArray *arrName;
                          arrName=[[result objectForKey:@"name"] componentsSeparatedByString:@" "];

                          [fbResultData setObject:[arrName objectAtIndex:0] forKey:@"name"];
                      if ([result objectForKey:@"last_name"]) {
                          [fbResultData setObject:[result objectForKey:@"last_name"] forKey:@"last_name"];
                      if ([result objectForKey:@"id"]) {
                          [fbResultData setObject:[result objectForKey:@"id"] forKey:@"id"];
                      [fbResultData setObject:accessToken forKey:@"Access Token"]; //m
                      FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                                                    initWithGraphPath:[NSString stringWithFormat:@"me/picture?type=large&redirect=false"]
                      [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                                            id result,
                                                            NSError *error) {
                          if (!error){
                              if ([[result objectForKey:@"data"] objectForKey:@"url"]) {
                                  [fbResultData setObject:[[result objectForKey:@"data"] objectForKey:@"url"] forKey:@"picture"];
                              NSLog(@"Final data of FB login********%@",fbResultData);
                          } }];
                  else {
                      NSLog(@"result: %@",[error description]);
                      UIAlertController *alert=[UIAlertController alertControllerWithTitle:NSLocalizedString(@"Error", nil) message:[error description] preferredStyle:UIAlertControllerStyleAlert];
                      UIAlertAction *alertBtnOk=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                                                     [alert dismissViewControllerAnimated:YES completion:nil];
                      [alert addAction:alertBtnOk];
                      [self presentViewController:alert animated:YES completion:nil];
             [[FBSDKLoginManager new] logOut];

In fbResultData you will get all user data, but don't forget to import these files:

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>