我正在为 iOS 开发一个应用程序,用户应该使用 facebook 登录。所以我正在尝试获取用户个人资料图片,但以下代码返回 "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)
}
})
更新
以下更改:
FBRequestConnection.startWithGraphPath("\(userID)/picture?type=large", completionHandler: { (connection, result, error) -> Void in
if (error? != nil){
NSLog("error = \(error)")
}else{
println(result)
}
})
正在返回:
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
最佳答案
你可以这样做:
// 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)
}
}
这是我获得 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")
}
}
})
}
我使用的是 Xcode 6.4 和 Swift 1.2
关于ios - 通过 Facebook Graph API 快速获取个人资料图片返回 "unsupported URL",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28333951/