Luki
Luki

Reputation: 447

Getting user's Personal info from FacebookSDK in iOS

I am trying getting info from Facebook API. The parameters are group in couple catalogs. To get data I using these code below from Facebook SDK:

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                           initWithGraphPath:@"me"
                                  parameters:@{ @"fields" : @"name, birthday"}
                                  HTTPMethod:@"GET"];[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                  id result,
                                  NSError *error) {

];

Name of catalog where are data 'name' and 'birthday' is "fields". But I want to get more data from other catalogs (edges, parameters), like first name, last name, email, id, about, etc. How can I write code to get it all?

Upvotes: 1

Views: 2209

Answers (2)

Rohit suvagiya
Rohit suvagiya

Reputation: 1015

Put this code

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
     {
         if (error)
         {
             // Process error
             NSLog(@"error is :%@",error);
         }
         else if (result.isCancelled)
         {
             // Handle cancellations
             NSLog(@"error is :%@",error);
         }
         else
         {
             if ([result.grantedPermissions containsObject:@"email"])
             {
                 [self fetchUserInfo];
             }
         }
    }];

you can get facebook user information as bellow

-(void)fetchUserInfo
{
    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 , friendlists"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {

                 NSString *photostring=[[[result valueForKey:@"picture"] objectForKey:@"data"] valueForKey:@"url"];
                 photostring = [photostring stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];

                 NSLog(@"all data here is:%@",result);
                 NSLog(@"username is :%@",[result valueForKey:@"name"]);
                 NSLog(@"PhotoUrl is :%@",photostring);
                 NSLog(@"mail id is :%@",[result valueForKey:@"email"]);
             }
         }];
    }
}

Good luck with your project

Upvotes: 1

Ferrakkem Bhuiyan
Ferrakkem Bhuiyan

Reputation: 2783

Firstly, you must put this code where login process start like bellow

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

[login logInWithReadPermissions:@[@"public_profile", @"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {

    if (error)
    {

     // There is an error here.

    }
    else
    {
        if(result.token)   // This means if There is current access token.
        {    
            // Token created successfully and you are ready to get profile info
            [self getFacebookProfileInfo];
        }        
    }
}];

if login successful , implement this method to get user's public profile

-(void)getFacebookProfileInfos { 

FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:nil];

FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];

[connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {

      if(result)
      {            
        if ([result objectForKey:@"email"]) {

          NSLog(@"Email: %@",[result objectForKey:@"email"]);

        }
        if ([result objectForKey:@"first_name"]) {

          NSLog(@"First Name : %@",[result objectForKey:@"first_name"]);

        }
         if ([result objectForKey:@"dob"]) {

          NSLog(@"Date of birth : %@",[result objectForKey:@"dob"]);

        }
        if ([result objectForKey:@"id"]) {

          NSLog(@"User id : %@",[result objectForKey:@"id"]);

        }

      }

 }];

[connection start];
}

you can also follow this link get facbook user information

or you can get facebook user information as bellow

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 *bateOfBirth = user.bate_Of_Birth;
             NSString *facebookId = user.id;
             NSString *email = [user objectForKey:@"email"];
             NSString *imageUrl = [[NSString alloc] initWithFormat: @"http://graph.facebook.com/%@/picture?type=large", facebookId];
         }
     }];
}

Upvotes: 0

Related Questions