0

how to get userprofile in ios

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 {


         /*"birthday"  will also be fethched*/

         /*https://developers.facebook.com/docs/android/graph*/


         NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
         [parameters setValue:@"id,name,email,gender,first_name,last_name" forKey:@"fields"];
         if ([FBSDKAccessToken currentAccessToken])
         {
             [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]
              startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
              {
                  if (!error)
                  {
                      NSLog(@"fetched user:%@", result);

                fbid          =     [result valueForKey:@"id"];
                fbName        =     [result valueForKey:@"name"];
                fbEmail       =     [result valueForKey:@"email"];
                fbGender      =     [result valueForKey:@"gender"];
                fbFirstname   =     [result valueForKey:@"first_name"];
                fbLastname    =     [result valueForKey:@"last_name"];


                     // [self fbLoginServerRequest];/********/

}
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143

3 Answers3

0
        FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
                [login logInWithReadPermissions:@[@"public_profile", @"email", @"user_friends"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
                    if (error) {
                        // Process error

                    } else if (result.isCancelled) {
                        // Handle cancellations


                    } else {

                        // Successfull login

                        if ([result.grantedPermissions containsObject:@"email"]) {

    if ([FBSDKAccessToken currentAccessToken]) {
                NSLog(@"YES");
                NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
                [parameters setValue:@"id,name,email,gender,picture" forKey:@"fields"];
                [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,                        id result, NSError *error) {
                    if (error) {
                        //                NSLog(@"%@",result);
                        viwLoginWave.hidden = YES;
                        [btnLogin setTitle:@"LOGIN" forState:UIControlStateNormal];
                        self.view.userInteractionEnabled = YES;
                    }

                    NSString *email1 = [result objectForKey:@"email"];
                    if (email1 == nil) {
                        [appDelegate showProgressWithMessage:@"Unable to access private account."];

                    }
                    else {
NSLog(@"You got your stuff.");
                    NSLog(@"%@",result);
                    }


                }];
            }
            else {
                NSLog(@"NO");
            }
                        }
                    }
                }];

In this printed result, You will get everything you want.

Other required things: Add following keys in info plist and save your app values. - Set value for FacebookAppID. - Set value for FacebookDisplayName - List item

Davinder Singh
  • 645
  • 3
  • 12
0

I am not sure why your code is not working. The code below is working for me after I subscribe to the FBSDKLoginButtonDelegate, and the user goes through the all shebang of granting permissions. If you are already signed in, all you need is the GraphPad request. This is assuming that the token is still valid. I would start checking the token validity using the Access Token Tool

- (void)loginButton:(FBSDKLoginButton*)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult*)result
  if(error) {
     NSLog(@"FB login error %@",error);

  } else if(result.isCancelled) {
     NSLog(@"FB login cancelled");

  } else {

    // Check if the user declined permissions
    if([result.declinedPermissions count] > 0) {

        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"picture, email, first_name, last_name"}] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
            NSLog(@"FBSDKGraphRequest ERROR %@", error);
            if (!error) {
                NSLog(@"fetched user: %@", result);

                NSURL *profileImageURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/me/picture?type=normal&return_ssl_resources=1&access_token=%@",accessToken]];
                NSData *imageData = [NSData dataWithContentsOfURL:profileImageURL];
                UIImage *image = [UIImage imageWithData:imageData];

                NSMutableDictionary *faceBookParametersDictionary = [[NSMutableDictionary alloc] init];
                [faceBookParametersDictionary setValue:image forKey:@"image"];
                [faceBookParametersDictionary setValue:userID forKey:@"id"];
                [faceBookParametersDictionary setValue:result[@"email"] forKey:@"email"];
                [faceBookParametersDictionary setValue:result[@"first_name"] forKey:@"firstname"];
                [faceBookParametersDictionary setValue:result[@"last_name"] forKey:@"lastname"];
                [faceBookParametersDictionary setValue:accessToken forKey:@"token"];
                NSLog(@"faceBookParametersDictionary %@", faceBookParametersDictionary);

            }
        }];
      }
    }
}
Alex
  • 995
  • 12
  • 25
0
@interface ViewController ()
{    


NSString *getFbid;
 NSString *getFbFirstName,*getFBlastName, *getFbemail,*getfbBirthday,*getfbGender,*getFBpHone,*getFBlocation,*getFBcountry;



}




 -(void)loginButtonClicked
{



NSUserDefaults *defFacebookData = [NSUserDefaults standardUserDefaults];


FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
 logInWithReadPermissions: @[@"public_profile",  @"user_friends", @"email"]
 fromViewController:self
 handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
     if (error) {
         DLog(@"Process error======%@",error.description);
         indicators.hidden=YES;
         [indicators stopAnimating];
     } else if (result.isCancelled) {
         DLog(@"Cancelled");
         indicators.hidden=YES;
         [indicators stopAnimating];
     } else {

         if ([FBSDKAccessToken currentAccessToken]) {



             [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , gender ,friendlists"}]
              startWithCompletionHandler:^(
                                           FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                  if (!error)
                  {

                      //  NSLog(@"fetched user:%@", result);

                      // [self fetchingFacebookFriends];

                      [defFacebookData setObject:[result objectForKey:@"email"] forKey:@"fbEmail"];

                      [defFacebookData setObject:[result objectForKey:@"id"] forKey:@"fbID"];


                      //PASS ID
                      getFbid  = [result objectForKey:@"id"];
    NSLog(@"getFbid========>%@",getFbid);

                      //PASS FIRST NAME
                      getFbFirstName=[result objectForKey:@"first_name"];

    NSLog(@"first======>%@",getFbFirstName);

                      //PASS LAST NAME
                      getFBlastName=[result objectForKey:@"last_name"];
    NSLog(@"first======>%@",getFBlastName);

                      //PASS EMAIL
                      getFbemail=[result objectForKey:@"email"];
    NSLog(@"first======>%@",getFbemail);

                      //PASS PHONE
                      getfbGender=[result objectForKey:@"gender"];
    NSLog(@"first======>%@",getfbGender);


                      [defFacebookData setObject:[result objectForKey:@"name"] forKey:@"fbName"];

                      // Image
                      FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                                                    initWithGraphPath:[NSString stringWithFormat:@"me/picture?type=large&redirect=false"]
                                                    parameters:nil
                                                    HTTPMethod:@"GET"];
                      [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                                            id fbImageResult,
                                                            NSError *error) {

NSString *strURL = [NSString stringWithFormat:@"%@",[[fbImageResult objectForKey:@"data"] objectForKey:@"url"]];

    NSLog(@"strURL=====>%@",strURL);

[defFacebookData setObject:strURL forKey:@"fbImage"];

                          [defFacebookData synchronize];


                          NSDictionary *fbdict=[NSDictionary dictionaryWithObjectsAndKeys:getFbid,@"id",getFbFirstName,@"first_name",getFBlastName,@"last_name",getFbemail,@"email",getfbGender,@"gender",strURL,@"fbImage", nil];

                          NSLog(@"done=========>%@",fbdict);



                      }];
                  }
                  else{

                      DLog(@"error is %@", error.description);
                  }
              }];
         }
     }
 }];

}
Dishant Rajput
  • 1,329
  • 1
  • 10
  • 20