Can't able to fetch the User Profile photo in ios

366 views Asked by At

I am trying to get the facebook account details using SLRequest in ios.

I got all the details But Can not able to get the Profile Photo link of the User.

My code is as follow:

NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
                         @"xxxxxxxxxxxxxx", ACFacebookAppIdKey,
                         [NSArray arrayWithObjects:@"email", nil] , ACFacebookPermissionsKey,
                         nil];

[_accountStore requestAccessToAccountsWithType:facebookTypeAccount
                                       options:options
                                    completion:^(BOOL granted, NSError *error) {
                                        if(granted){
                                            NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
                                            _facebookAccount = [accounts lastObject];
                                            NSLog(@"Success");

                                            [self me];
                                        }else{
                                            // ouch
                                            NSLog(@"Fail");
                                            NSLog(@"Error: %@", error);
                                        }
                                    }];


- (void)me{
NSURL *meurl = [NSURL URLWithString:@"https://graph.facebook.com/me"];


    SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                              requestMethod:SLRequestMethodGET
                                                        URL:meurl
                                                 parameters:nil];

    merequest.account = _facebookAccount;

    [merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

        NSLog(@"%@", meDataString);

        NSJSONSerialization *js=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",js);

    }];

}

Help me to solve this.

1

There are 1 answers

10
MuhammadBassio On BEST ANSWER

You can get the user's ID from the returned response, then use the below to get the profile picture:

http://graph.facebook.com/User_ID/picture

I use SDWebImage for images:

NSString *urlstr = @"http://graph.facebook.com/100001393655684/picture";

[imageView setImageWithURL:[NSURL URLWithString:urlstr]
                   placeholderImage:nil];

For Twitter, please make sure you authenticate your request, check this link for more details

Note: You may need to update TWRequest to SLRequest if you aren't deploying for iOS5