Berry Blue
Berry Blue

Reputation: 16542

Get profile picture for other Facebook users in iOS

I'm trying to get the profile image for a Facebook user using the Graph API in the Facebook SDK for iOS but the example code from the website does not work for me. https://developers.facebook.com/docs/graph-api/reference/user/picture/

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
    initWithGraphPath:@"/123456789/picture"
           parameters:nil
           HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
    // Insert your code here
}];

I get the following error in the console.

FBSDKLog: starting with Graph API v2.4, GET requests for //123456789/picture should contain an explicit "fields" parameter

From my understanding of the error message instead of parameters:nil I need to enter something like parameters:@{@"fields": @"id"} but whatever I try result in the completing handler is always nil. I don't know what to enter for the profile picture.

EDIT: I'm looking to get the profile picture for other users.

Upvotes: 0

Views: 322

Answers (2)

Swati Gautam
Swati Gautam

Reputation: 191

You can use the following code for getting the data from the facebook login:-

-(void)getFacebookProfileInfos { FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:@{@"fields": @"id,email,name,birthday,first_name,last_name,gender,picture.type(large)"}];

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

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

    if(result)
    {
        dictUserData = [[NSDictionary alloc]init];
        dictUserData=result;
    }
}];
[connection start];

}

you will get all the data in the "dictUserData" dictionary and from that you will access the facebook profile in this way:-

UIImage * imgProfile = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", dictUserData[@"picture"][@"data"][@"url"]]]]]; NSString * strImageData = [self imageToNSString:imgProfile];

Upvotes: 0

kalpesh satasiya
kalpesh satasiya

Reputation: 817

Try This One:-

- (IBAction)btnFaceBook:(id)sender {



    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login setLoginBehavior:FBSDKLoginBehaviorBrowser];
    [login logInWithReadPermissions:@[@"public_profile",@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error)
        {
            [[FBSDKLoginManager new]logOut];

        } else if (result.isCancelled)
        {
            // Handle cancellations
        }
        else
        {
            if ([result.grantedPermissions containsObject:@"email"])
            {
                [self getDataFromFB];
            }
        }
    }];
}


-(void)getDataFromFB
{
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view  animated:true];

    if ([FBSDKAccessToken currentAccessToken]) {
         [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:[NSDictionary dictionaryWithObject:@"political,picture.width(500).height(500),id,email,first_name,last_name,gender,name" forKey:@"fields"]]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
         {
             if (!error && [result count] != 0)
             {
                 [hud hideAnimated:true];
                 NSLog(@"%@", result);

             }
             else
             {
                  [hud hideAnimated:true];
                  [self displayAlertMesage:@"Some error has been occure." title:@"Oops!"];
             }
        }];
     }
 }

Upvotes: 1

Related Questions