0

I am using interested in obtaining the following info from a user's fb profile :

  1. email
  2. name
  3. profile picture link
  4. 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 = "dedewdd@gmail.com";
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.

Alk
  • 5,215
  • 8
  • 47
  • 116

4 Answers4

2

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{

}
}
Andrew Wormald
  • 219
  • 2
  • 9
1

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)")
                
            }
        })
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Sanjeet Verma
  • 551
  • 4
  • 15
  • if you look at the JSON response I posted, how would you extract the min and max values from the age_range? – Alk May 24 '16 at 13:53
0

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..enter image description here

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) {

}]

0

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