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.
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.
Select "Options" from the horizontal menu and click "Make Profile Picture" to make the picture your default profile picture.
Tap your current profile picture. Tap Edit End Time. Tap Switch to previous picture now. Tap Set in the top right.
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:
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]; } }]; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With