阅读Swift 4中Facebook Graph Request的回复

时间:2017-11-27 02:18:35

标签: ios swift facebook facebook-graph-api

我正在尝试使用Graph API将Facebook好友列表添加到我的Swift iOS应用程序中。

我正在努力研究如何实际访问从Facebook发回的数据。

我得到的回答如下:

success(FacebookCore.GraphResponse(rawResponse: Optional({
"first_name" = Jamie;
id = 1626917907360895;
"last_name" = McAllister;
name = "Jamie McAllister";
picture =     {
    data =         {
        height = 50;
        "is_silhouette" = 0;
        url = "https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/12994335_1101318013254223_4481895970110564364_n.jpg?oh=d10209f113213981e4417e7f6f3f82d8&oe=5A91F135";
        width = 50;
    };
};
})))

我的图表请求目前仅为/me,因为我是唯一的注册用户。但是响应中的所有字段都是我将从朋友列表中请求的

图形请求如下所示:

var graph = GraphRequest.init(graphPath: "me")
    graph.parameters = ["fields": "id, first_name, last_name, middle_name, name, email, picture"]

    graph.start({ (response, data) in
        print("======GRAPH=====")
        print(data)

        })

所以,我希望能够获取GraphResponse,使其成为一个数组并将其分配给变量。我完全被这个难倒了。

3 个答案:

答案 0 :(得分:3)

希望这会有所帮助 Swift 4.2

    func getFbId(){
    if(AccessToken.current != nil){
        let req = GraphRequest(graphPath: "me", parameters: ["fields": "email,first_name,last_name,gender,picture"], accessToken: AccessToken.current, httpMethod: GraphRequestHTTPMethod(rawValue: "GET")!)
        req.start({ (connection, result) in
            switch result {
            case .failed(let error):
                print(error)

            case .success(let graphResponse):
                if let responseDictionary = graphResponse.dictionaryValue {
                    print(responseDictionary)
                    let firstNameFB = responseDictionary["first_name"] as? String
                    let lastNameFB = responseDictionary["last_name"] as? String
                    let socialIdFB = responseDictionary["id"] as? String
                    let genderFB = responseDictionary["gender"] as? String
                    let pictureUrlFB = responseDictionary["picture"] as? [String:Any]
                    let photoData = pictureUrlFB!["data"] as? [String:Any]
                    let photoUrl = photoData!["url"] as? String
                    print(firstNameFB, lastNameFB, socialIdFB, genderFB, photoUrl)
                }
            }
        })
    }
}

答案 1 :(得分:1)

要阅读Facebook Graph Request的回复,请使用以下代码行

let info = data as! [String : AnyObject]
 if info["name"] as? String != nil {
   self.usernameLbl.text = info["name"] as! String
 }

希望这会对你有所帮助

答案 2 :(得分:0)

图形请求开始完成块已更改为需要3个参数,而不是2个。

所以您会遇到类似的情况(现在开始块中已通过了3个项目 [连接,结果和错误])

let params = ["fields" : "email, first_name, id, picture"]
    let graphRequest = GraphRequest(graphPath: "me", parameters: params)
    graphRequest.start{
        (connection, result, error in

        if error == nil {
           if let responseDictionary = result as? NSDictionary {

            UserProfile.userName = responseDictionary["first_name"] as? String ?? "User"


           UserProfile.userID = responseDictionary["id"] as! String

           var pictureUrl = ""
           if let picture = responseDictionary["picture"] as? NSDictionary, let data = picture["data"] as? NSDictionary, let url = data["url"] as? String {
                                        pictureUrl = url

                                    }
                                }
        }else{
            print("error in graph request:", error)

        }