我想从Facebook获取我的头像图片,我试过这样:
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
import FBSDKShareKit
import FBSDKMessengerShareKit
class HomeViewController: UIViewController {
@IBOutlet var avatarImageView: UIImageView!
var id = ""
override func viewDidLoad() {
super.viewDidLoad()
addBackgroundImage()
let accessToken = FBSDKAccessToken.currentAccessToken()
if accessToken != nil {
print(accessToken.tokenString)
}
// let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,name"], tokenString: accessToken.tokenString, version: nil, HTTPMethod: "GET")
// req.startWithCompletionHandler({ (connection, result, error : NSError!) -> Void in
// error == nil ? print("result \(result)") : print("error \(error)")
// })
returnUserData()
}
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!) {
avatarImageView.image = UIImage(data: data)
}
}
func returnUserData()
{
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
print("fetched user: \(result)")
if let id: NSString = result.valueForKey("id") as? NSString {
print("ID is: \(id)")
self.returnUserProfileImage(id)
} else {
print("ID es null")
}
}
})
}
func addBackgroundImage() {
UIGraphicsBeginImageContext(self.view.frame.size)
UIImage(named: "blur_background")?.drawInRect(self.view.bounds)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
view.backgroundColor = UIColor(patternImage: image)
}
}
但它说:
2016-04-10 22:41:51.400 GiftsToMyFriends [14444:260220] FBSDKLog: 从Graph API v2.4开始,对/ me的GET请求应该包含一个 显式“fields”参数获取用户:{ id = 942489539201413; name =“\ U0110 \ U0103ng Kh \ U01b0 \ U01a1ng Ph \ U1ea1m”; ID是:942489539201413
2016-04-10 22:41:51.639 GiftsToMyFriends [14444:260381] App Transport 安全性阻止了明文HTTP(http://)资源加载 是不安全的。可以通过您的应用程序配置临时例外 Info.plist文件。
我的Info.plist已经添加了这个:
图片没有出现在我的ViewController上。