Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting profile picture with swift through Facebook Graph API return "unsupported URL"

I'm developing an app for iOS that user should login with facebook. So i'm trying to get the users profile picture but the following code is returning "unsupported URL"

FBRequestConnection.startWithGraphPath("\(userID)/picture?type=large&access_token=207613839327374|BgKi3AePtvg1oDO8GTbWqqLE_SM", completionHandler: { (connection, result, error) -> Void in
   if (error? != nil){
      NSLog("error = \(error)")
   }else{
      println(result)
   }
})

UPDATE

The following change:

FBRequestConnection.startWithGraphPath("\(userID)/picture?type=large", completionHandler: { (connection, result, error) -> Void in
  if (error? != nil){
    NSLog("error = \(error)")
  }else{
    println(result)
  }
})

Is returning:

error = Error Domain=com.facebook.sdk Code=6 "Response is a non-text MIME type; endpoints that return images and other binary data should be fetched using NSURLRequest and NSURLConnection" UserInfo=0x786d4790
like image 504
adolfosrs Avatar asked Feb 05 '15 00:02

adolfosrs


1 Answers

You can do it in this way:

    // accessToken is your Facebook id
    func returnUserProfileImage(accessToken: NSString)
    {
        var userID = accessToken as NSString
        var facebookProfileUrl = NSURL(string: "http://graph.facebook.com/\(userID)/picture?type=large")

        if let data = NSData(contentsOfURL: facebookProfileUrl!) {
            imageProfile.image = UIImage(data: data)
        }

    }

This is the way I obtained my Facebook id:

func returnUserData()
{
    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

        if ((error) != nil)
        {
            // Process error
            println("Error: \(error)")
        }
        else
        {
            println("fetched user: \(result)")

            if let id: NSString = result.valueForKey("id") as? NSString {
                println("ID is: \(id)")
                self.returnUserProfileImage(id)
            } else {
                println("ID es null")
            }


        }
    })
}

I was using Xcode 6.4 and Swift 1.2

like image 193
Jorge Casariego Avatar answered Oct 08 '22 01:10

Jorge Casariego