Getting Username and Profile Picture from Facebook iOS 7

Getting username and profile picture from Facebook iOS 7

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

Getting and displaying a user's profile picture in the iOS Facebook SDK

import FacebookCore
import FacebookLogin

Profile.loadCurrentProfile(completion: { profile, error in
if let profile = profile {
let imageURL = profile.imageURL(forMode: .square, size: CGSize(width: 200.0, height: 200.0))
}
})

iOS7 Access user Facebook Profile Picture

Because you are using /me/ in your API call, an access_token is required because the API doesn't know who me is. If you replace this with a User ID, e.g.

https://graph.facebook.com/v2.1/4/picture?type=large

It should work fine.

If you want to continue using /me/ in the URL, just append the user's access_token to the URL too, e.g.:

https://graph.facebook.com/v2.1/4/picture?type=large&access_token=abcdef

Facebook iOS SDK and swift: how get user's profile picture

The profile picture is in fact public and you can simply by adding the user id to Facebook's designated profile picture url address, ex:

var userID = user["id"] as NSString     
var facebookProfileUrl = "http://graph.facebook.com/\(userID)/picture?type=large"

This particular url address should return the "large" version of the user's profile picture, but several more photo options are available in the docs.

how to fetch profile picture from facebook

If you are able to log in with Facebook, then you should have the FB ID of that user. With that in hand, you can use this for get the profile picture:

func getProfPic(fid: String) -> UIImage? {
if (fid != "") {
var imgURLString = "http://graph.facebook.com/" + fid! + "/picture?type=large" //type=normal
var imgURL = NSURL(string: imgURLString)
var imageData = NSData(contentsOfURL: imgURL!)
var image = UIImage(data: imageData!)
return image
}
return nil
}

You can also change the type parameter to a different size in the URL.

Get Facebook API Profile Picture ios Swift

So I use Kingfisher to download the image but essentially the NSURL is the key to getting a larger image. IF this doesnt work, then try changing the "50x50" in the url to "250x250" and see if that works

if FBSDKAccessToken.currentAccessToken() != nil {

let userID = FBSDKAccessToken.currentAccessToken().userID

if(userID != nil) //should be != nil
{
print(userID)
}

let URL = NSURL(string: "http://graph.facebook.com/\(userID)/picture?type=large")!

profileImage.kf_setImageWithURL(URL)

}else{

}
}

Get user profile picture by Id

http://graph.facebook.com/" + facebookId + "/picture?type=square
For instance:
http://graph.facebook.com/67563683055/picture?type=square

There are also more sizes besides "square". See the docs.

Update September 2020

As Facebook have updated their docs this method is not working anymore without a token. You need to append some kind of access_token. You can find further information and how to do it correctly in the fb docs to the graph api of user picture

Requirements Change
This endpoint supports App-Scoped User IDs (ASID), User IDs (UID), and Page-Scoped User IDs (PSID). Currently you can query ASIDs and UIDs with no requirements. However, beginning October 24, 2020, an access token will be required for all UID-based queries. If you query a UID and thus must include a token:

use a User access token for Facebook Login authenticated requests

use a Page access token for page-scoped requests

use an App access token for server-side requests

use a Client access token for mobile or web client-side requests

Quote of fb docs



Related Topics



Leave a reply



Submit