0

Okay so when I try to recieve the users profil picture, the picture returns a white box with a question mark in it?

Heres my code:

func getProfilePic(fid: String) -> SKTexture? {
  let imgURL = NSURL(string: "http://graph.facebook.com/" + fid + "/picture?type=large")
  let imageData = NSData(contentsOfURL: imgURL!)
  let imageUI = UIImage(data: imageData!)
  let image = SKTexture(image: imageUI!)
  return image
}

func getFBUserData() {
  if((FBSDKAccessToken.currentAccessToken()) != nil) {
    FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email, picture"]).startWithCompletionHandler({ (connection, result, error) -> Void in
      if (error == nil){
        print(result)
        if let userData = result as? NSDictionary {
          personalUserID = userData["id"] as! String
        }
      } else {
        print("error")
      }
    })
  }
  picture.texture = getProfilePic("\(personalUserID)")

How do I get it to show the right picture?

Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
Benja0906
  • 1,437
  • 2
  • 15
  • 25

1 Answers1

2

I'm guessing your profile picture is not available for the public. Now you're not providing an access token with the request, so the request is handled as unauthorised - so you see only what the public eye sees.

To fix it:

func getProfilePic(fid: String) -> SKTexture? {
    let imgURL = NSURL(string: "http://graph.facebook.com/" + fid + "/picture?type=large&access_token=" + FBSDKAccessToken.currentAccessToken().tokenString)
    let imageData = NSData(contentsOfURL: imgURL!)
    let imageUI = UIImage(data: imageData!)
    let image = SKTexture(image: imageUI!)
    return image
}

Also, you'd want to use https and the current API version v2.5 to make the requests, otherwise your code might break in any second when Facebook makes changes. So, with that in mind:

func getProfilePic(fid: String) -> SKTexture? {
    let imgURL = NSURL(string: "https://graph.facebook.com/v2.5/" + fid + "/picture?type=large&access_token=" + FBSDKAccessToken.currentAccessToken().tokenString)
    let imageData = NSData(contentsOfURL: imgURL!)
    let imageUI = UIImage(data: imageData!)
    let image = SKTexture(image: imageUI!)
    return image
}

That should do the trick.

jehna1
  • 3,110
  • 1
  • 19
  • 29
  • It instaly crashes when i try that, it gives me this error CUICatalog: Invalid Request: requesting subtype without specifying idiom fatal error: unexpectedly found nil while unwrapping an Optional value. but when you say I want to use https, what do you mean then? – Benja0906 Mar 11 '16 at 18:35
  • @Benja0906 The error means that you're not logged in with Facebook when the request is made (so the access token is nil). You should do the request only after you have an access token. – jehna1 Mar 11 '16 at 19:08
  • @Benja0906 The https question: I saw you were using the API url that started with `http://`. You should always use `https://` when it is available, beause it prevents someone from hijacking sensitive information. Also the Facebook Graph API v2.5 doesn't allow authorised requests on non-https connections. – jehna1 Mar 11 '16 at 19:12
  • well i should be logged in, i have this in my didMoveToView if (FBSDKAccessToken.currentAccessToken() != nil) { print("Logged in..." } but its stil saying this: Logged in... 2016-03-11 20:41:48.518 Brainy[3750:1837721] CUICatalog: Invalid Request: requesting subtype without specifying idiom fatal error: unexpectedly found nil while unwrapping an Optional value – Benja0906 Mar 11 '16 at 19:46
  • @Benja0906 all I can say is that when I run the code, it fetches the correct picture just fine. The error `found nil while unwrapping an Optional value` error basically means that something is `nil` that should not be `nil`. – jehna1 Mar 11 '16 at 19:51