3

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");
             }
         }];
    }
Andrew
  • 227,796
  • 193
  • 515
  • 708
Aarti Oza
  • 1,134
  • 2
  • 14
  • 31

3 Answers3

2
    if ([FBSDKAccessToken currentAccessToken]) {
    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{ @"fields" : @"id,name,picture.width(100).height(100)"}]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
        if (!error) {
            NSString *nameOfLoginUser = [result valueForKey:@"name"];
            NSString *imageStringOfLoginUser = [[[result valueForKey:@"picture"] valueForKey:@"data"] valueForKey:@"url"];
            NSURL *url = [[NSURL alloc] initWithURL: imageStringOfLoginUser];
            [self.imageView setImageWithURL:url placeholderImage: nil];
        }
    }];
}
Muhammad Rizwan
  • 3,470
  • 1
  • 27
  • 35
Avinash651
  • 1,399
  • 11
  • 17
  • hi.. can you answer this question http://stackoverflow.com/questions/33802803/facebook-app-invites-notification-not-working-in-ios. Please help me If you can. – Aarti Oza Nov 19 '15 at 11:42
1

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"];

}

 }]; 
Rumin
  • 3,787
  • 3
  • 27
  • 30
  • hi.. can you answer this question http://stackoverflow.com/questions/33802803/facebook-app-invites-notification-not-working-in-ios. Please help me If you can. – Aarti Oza Nov 19 '15 at 11:42
1
- (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 %@ %@", result.token.userID, result.token.tokenString);
             [self fetchUserInfo];
         }
     }];
}

-(void)fetchUserInfo
{
    if ([FBSDKAccessToken currentAccessToken])
    {
        NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);

        [[[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)
             {
                 NSLog(@"resultis:%@",result);
             }
             else
             {
                 NSLog(@"Error %@",error);
             }
         }];

    }

}
Aarti Oza
  • 1,134
  • 2
  • 14
  • 31