'Get Facebook API Profile Picture ios Swift
I am using interested in obtaining the following info from a user's fb profile :
- name
- profile picture link
- age
To do so I use the following code:
let accessToken = FBSDKAccessToken.currentAccessToken()
let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"picture.redirect(false),email,name,gender,age_range,birthday"], tokenString: accessToken.tokenString, version: nil, HTTPMethod: "GET")
req.startWithCompletionHandler({ (connection, result, error : NSError!) -> Void in
if(error == nil)
{
print("result \(result)")
}
else
{
print("error \(error)")
}
}) }
This returns:
result {
"age_range" = {
min = 21;
};
email = "[email protected]";
gender = male;
id = xxxxxxxxxxxx;
name = "John Smith";
picture = {
data = {
"is_silhouette" = 0;
url = "https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/dwqdwddqwdwqdw";
};
};
}
The problem with this is that this image is 50X50 pixels, I would like to obtain a link to a larger image.
Solution 1:[1]
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{
}
}
Solution 2:[2]
Try this:
@IBAction func FacebookLogin(sender: AnyObject) {
var message = String()
if Reachability.isConnectedToNetwork() == true {
let loginView : FBSDKLoginManager = FBSDKLoginManager()
loginView.loginBehavior = FBSDKLoginBehavior.Web
if (FBSDKAccessToken .currentAccessToken() != nil) {
loginView.logOut()
}else{
loginView.logInWithReadPermissions(["email","public_profile"], fromViewController: self, handler: { (result:FBSDKLoginManagerLoginResult!, error:NSError!) in
if (error != nil){
print("Login Process Eroor!"+error.localizedDescription)
}else if result.isCancelled{
print("User cancled Login")
}else{
print("Login Success")
if result.grantedPermissions .contains("email"){
self.fetchUserInfo()
}else{
message = message.stringByAppendingString("Facebook email permission error")
WebService.ShowToastMessage(message, viewcontroller: self)
}
}
})
}
} else {
message = message.stringByAppendingString("Make sure your device is connected to the internet.")
WebService.ShowToastMessage(message, viewcontroller: self)
}
}
func fetchUserInfo() -> Void {
if (FBSDKAccessToken .currentAccessToken() != nil) {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id,gender,birthday,email,name,picture.width(480).height(480)"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
print("Error: \(error.localizedDescription)")
}
else
{
print("fetched user: \(result)")
let id : NSString = result.valueForKey("id") as! String
let name:NSString = result.valueForKey("name") as! String
let gender:NSString = result.valueForKey("gender") as! String
let email:NSString = result.valueForKey("email") as! String
print("User ID is: \(id)")
print("User email is:\(email)")
print("User name is :\(name)")
print("User gender is :\(gender)")
}
})
}
}
Solution 3:[3]
According to the facebook docs
https://developers.facebook.com/docs/graph-api/reference/user/picture/
You can pass width and height of image,following are the list of params..
So you can try something like ..
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{ @"fields" : @"id,name,picture.width(100).height(100)"}]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
}]
Solution 4:[4]
Use this code for getting user id, name and profilepicture with one request : https://stackoverflow.com/a/45462114/1168602
Tested on xcode 8.3.3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrew Wormald |
| Solution 2 | halfer |
| Solution 3 | chitnisprasanna |
| Solution 4 | Sachindra Pandey |
