iosfbsdkfbsdkloginkit

Get User name and Profile picture using FBSDKLoginkit


I want to get Username and Profile picture usig FBSDKLoginKit. How to get it ? My code is here.

pod 'FBSDKLoginKit', '~> 4.7'

   - (IBAction)fbLoginActionClicked:(id)sender
    {
        FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

        [login
         logInWithReadPermissions: @[@"public_profile"]
         fromViewController:self
         handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
             if (error) {
                 NSLog(@"Process error");
             }
             else if (result.isCancelled)
             {
                 NSLog(@"Cancelled");
             }
             else
             {
                 NSLog(@"Logged in");
             }
         }];
    }

Solution

  • Use FBSDKGraphRequest like the following code to get username and profile picture of Facebook user.

    Once you are logged in, add the following code:

    NSLog(@"Logged in");
                 if ([result.grantedPermissions containsObject:@"public_profile"]) {
                     if ([FBSDKAccessToken currentAccessToken]) {
                         NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
                         [parameters setValue:@"name,picture.type(large)" forKey:@"fields"];
    
                         [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]
                          startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                              if (!error)
                              {
                                  NSLog(@"fetched user:%@", result);
    
    
                                  NSString *name = [[result valueForKey:@"name"] lowercaseString];
                                  NSString *username = [name stringByReplacingOccurrencesOfString:@" " withString:@""];
                                  NSArray *picture_arr = [result objectForKey:@"picture"];
                                  NSArray *data_arr = [picture_arr valueForKey:@"data"];
                                  NSString *profile_pic = [data_arr valueForKey:@"url"];
    
    }
    
     }];