Reputation: 9389
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)
}
})
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
Upvotes: 11
Views: 16847
Reputation: 6861
My example. Please do not pay attention on User
import Foundation
import FBSDKCoreKit
import FBSDKLoginKit
import SVProgressHUD
import SDWebImage
class FacebookManager {
// MARK: - functions
static func getFacebookProfileData(comletion: ((user: User?, error: NSError?) -> ())?) {
SVProgressHUD.showWithStatus(StringModel.getting)
let loginManager = FBSDKLoginManager()
loginManager.loginBehavior = .SystemAccount
loginManager.logInWithReadPermissions(nil, fromViewController: nil) { (tokenResult, error) in
if error == nil {
guard let token = tokenResult?.token?.tokenString else {
SVProgressHUD.dismiss()
return
}
getFacebookProfile(token, completion: { (error, user) in
if error == nil {
comletion?(user: user, error: error)
SVProgressHUD.dismiss()
} else {
SVProgressHUD.dismiss()
print(error?.localizedDescription)
}
})
} else {
SVProgressHUD.dismiss()
print(error.localizedDescription)
}
}
}
private static func getFacebookProfile(token: String, completion: ((error: NSError?, user: User?) -> ())?) {
FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "email, name"], HTTPMethod: "GET").startWithCompletionHandler { (requestConnection, result, error) in
if error == nil {
guard let resultDictionary = result as? [String : AnyObject] else { return }
guard let email = resultDictionary["email"] as? String else { return }
guard let id = resultDictionary["id"] as? String else { return }
guard let name = resultDictionary["name"] as? String else { return }
getFacebookProfileImage(id, completion: { (image, error) in
if error == nil {
let user = User(facebookName: name, facebookID: id, facebookEmail: email, facebookProfileImage: image)
completion?(error: nil, user: user)
}
})
} else {
print(error.localizedDescription)
completion?(error: nil, user: nil)
}
}
}
private static func getFacebookProfileImage(userID: String, completion: ((image: UIImage?, error: NSError?) -> ())) {
guard let facebookProfileImageURL = NSURL(string: "https://graph.facebook.com/\(userID)/picture?type=large") else { return }
print(facebookProfileImageURL)
let sdImageManager = SDWebImageManager.sharedManager()
sdImageManager.downloadImageWithURL(facebookProfileImageURL, options: .AvoidAutoSetImage, progress: nil) { (image, error, cachedType, bool, url) in
if error == nil {
completion(image: image, error: nil)
} else {
completion(image: nil, error: error)
print(error.localizedDescription)
}
}
}
}
Upvotes: 1
Reputation: 7499
Using PFFacebookUtils you can get a profile picture like this:
let pictureRequest = FBSDKGraphRequest(graphPath: "me/picture?type=normal&redirect=false", parameters: nil)
pictureRequest.startWithCompletionHandler({
(connection, result, error: NSError!) -> Void in
if error == nil && result != nil {
let imageData = result.objectForKey("data") as! NSDictionary
let dataURL = data.objectForKey("url") as! String
let pictureURL = NSURL(string: dataURL)
let imageData = NSData(contentsOfURL: pictureURL!)
let image = UIImage(data: imageData!)
}
})
Upvotes: 2
Reputation: 22212
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
Upvotes: 12
Reputation: 354
This line of code is working fine for getting profile Pic.
@IBOutlet var profilePic: UIImageView!
func loginViewFetchedUserInfo(loginView: FBLoginView!, user: FBGraphUser!) {
println("User:\(user)")
println("User ID:\(user.objectID)")
println("User Name:\(user.name)")
var userEmail = user.objectForKey("email") as String
println("User Email:\(userEmail)")
// Get user profile pic
let url = NSURL(string: "https://graph.facebook.com/\(user.objectID)/picture?type=large")
let urlRequest = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue()) { (response:NSURLResponse!, data:NSData!, error:NSError!) -> Void in
// Display the image
let image = UIImage(data: data)
self.profilePic.image = image
}
}
Upvotes: 1
Reputation: 7147
Able to get it using this code:
// Get user profile pic
var fbSession = PFFacebookUtils.session()
var accessToken = fbSession.accessTokenData.accessToken
let url = NSURL(string: "https://graph.facebook.com/me/picture?type=large&return_ssl_resources=1&access_token="+accessToken)
let urlRequest = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue()) { (response:NSURLResponse!, data:NSData!, error:NSError!) -> Void in
// Display the image
let image = UIImage(data: data)
self.imgProfile.image = image
}
Upvotes: 10
Reputation: 1288
You can simply get the profile pic by using the following url: https://graph.facebook.com/userID/picture?type=large
Where userID is your user's Facebook ID.
Upvotes: 2