ios - 在 Swift 4 中读取来自 Facebook Graph Request 的响应

标签 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,因为我是唯一的注册用户。但是响应中的所有字段都是我将从 friend 列表中请求的内容

图形请求如下所示:

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,将其设为一个数组并将其分配给一个变量。我完全被这个难住了。

最佳答案

希望对您有所帮助 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)
                }
            }
        })
    }
}

关于ios - 在 Swift 4 中读取来自 Facebook Graph Request 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47503092/

相关文章:

iOS 从 UITextView 中删除单词

iOS swift 为什么两种类型的闭包不能添加到数组

ios - TableView 快速复制同一标签的多个副本

javascript - 将访问 token 从 Facebook JS sdk 传递到 PHP session 或 cookie

ios - 用户使用 Facebook 登录后应用程序崩溃

iphone - 是否有可能让 iOS Simulator 的表现更像一部真正的 iPhone?

ios - 室内导航如何在 iOS 中使用 iBeacon 或 wi-fi 工作?

ios - 如何使用相机谷歌地图 xcode 移动标记(图钉)

swift - NotificationCenter 观察员没有收到帖子

ios - Facebook 登录在冷启动时崩溃