Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting username and profile picture from Facebook iOS 7

I have read a lot of tutorials about getting information from Facebook, but I have failed so far. I just want to get username and profile picture from Facebook.

- (IBAction)login:(id)sender {     [FBSession openActiveSessionWithReadPermissions:@[@"email",@"user_location",@"user_birthday",@"user_hometown"]                                    allowLoginUI:YES                               completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {     switch (state) {       case FBSessionStateOpen:          [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {             if (error) {                NSLog(@"error:%@",error);             } else {                // retrive user's details at here as shown below                NSLog(@"FB user first name:%@",user.first_name);                NSLog(@"FB user last name:%@",user.last_name);                NSLog(@"FB user birthday:%@",user.birthday);                NSLog(@"FB user location:%@",user.location);                NSLog(@"FB user username:%@",user.username);                NSLog(@"FB user gender:%@",[user objectForKey:@"gender"]);                NSLog(@"email id:%@",[user objectForKey:@"email"]);                NSLog(@"location:%@", [NSString stringWithFormat:@"Location: %@\n\n",                                                                          user.location[@"name"]]);               }         }];         break;         case FBSessionStateClosed:         case FBSessionStateClosedLoginFailed:            [FBSession.activeSession closeAndClearTokenInformation];         break;         default:         break;        }     } ];    } 

I used this code for get information, but I cannot get the any information. Can you help me about it? or Can you prefer a tutorial to read it? I have read tutorials on developer.facebook.com.

Thank you for your interest.

like image 727
Le'Kirdok Avatar asked Dec 17 '13 00:12

Le'Kirdok


People also ask

How do I get a PFP from Facebook?

Tap in the top right of Facebook, then tap your name. Tap your profile picture. Choose to Take Photo, Upload Photo, Add Frame or View Profile Picture. Tap Save.

How do I find my default PFP on Facebook?

Select "Options" from the horizontal menu and click "Make Profile Picture" to make the picture your default profile picture.

How do you reuse a profile picture on Facebook on iPhone?

Tap your current profile picture. Tap Edit End Time. Tap Switch to previous picture now. Tap Set in the top right.


2 Answers

This is the simplest way I've found to get the user's profile picture.

[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *FBuser, NSError *error) {     if (error) {       // Handle error     }      else {       NSString *userName = [FBuser name];       NSString *userImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", [FBuser objectID]];     }   }]; 

Other query parameters that can be used are:

  • type: small, normal, large, square
  • width: < value >
  • height: < value >
    • Use both width and height to get a cropped, aspect fill image
like image 123
Guilherme Avatar answered Oct 06 '22 05:10

Guilherme


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];         }     }]; } 
like image 24
Hemanshu Liya Avatar answered Oct 06 '22 04:10

Hemanshu Liya