Swift FacebookSDK Graphrequest完成处理程序未调用

时间:2015-10-30 10:51:02

标签: ios swift facebook-sdk-4.0 fbsdk

如果我直接在viewController上实现以下函数,一切正常。 (这不完整,但它适用于图片)

func getFBData() {

if((FBSDKAccessToken.currentAccessToken()) != nil){
    FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, email, first_name, last_name, picture"]).startWithCompletionHandler({ (connection, result, error) -> Void in
        if (error == nil){
            let email = result.valueForKey("email") as? String
            let fbid = result.valueForKey("id") as! String
            let fname = result.valueForKey("first_name") as! String
            let lname = result.valueForKey("last_name") as! String
            let facebookProfileUrl = "http://graph.facebook.com/\(fbid)/picture?type=large"

            let url = NSURL(string: facebookProfileUrl)

            if let data = NSData(contentsOfURL: url!) {
                self.imgProfile.image = UIImage(data: data)
            }
        }
    })
}

}

但我试图将所有与facebook相关的内容分开放在 FacebookController.swift 类中。

class func getUserData() -> [String: String] {

    var dict: [String: String]?
    let token = FBSDKAccessToken.currentAccessToken()
    let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, email, name, first_name, last_name, picture"])

    if((token) != nil) {
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
            if (error == nil) {

                let fbid = result.valueForKey("id") as! String
                let name = result.valueForKey("name") as? String
                let fname = result.valueForKey("first_name") as! String
                let lname = result.valueForKey("last_name") as! String
                let email = result.valueForKey("email") as? String
                let pictureURL = "http://graph.facebook.com/\(fbid)/picture?type=large"

                dict = ["id": fbid, "name": name!, "fname": fname, "lname": lname, "email": email!, "picture": pictureURL]
                print(dict)
            } else {
                print("ok")
                print("\(error)")
            }

        })

    }
    return dict!
}

用户已登录并且已授予所有权限,但当我尝试呼叫FacebookController()getUserData()时,它将返回dict,即nil。我已经认为这是因为graphRequest completionHandler 永远不会被调用。

有人可以帮忙吗?

0 个答案:

没有答案