首页 > 解决方案 > 在 iOS Swift 中获取 Facebook 封面照片 URL

问题描述

当使用 Facebook 的图形资源管理器图形路径时,我能够使用以下方法获取用户的封面照片 URL:

        connection.add(GraphRequest(graphPath: "/me/albums")) { httpResponse, result in ...

然后找到名称为“Cover Photos”的 id 并使用图形请求:

    connection.add(GraphRequest(graphPath: ("/" + id + "/photos"))) { httpResponse, result in

然后从封面照片相册中获取第一个和最近的 id

然后最后使用图形请求:

        connection.add(GraphRequest(graphPath: ("/" + id + "/picture"))) { httpResponse, result in

获取封面照片的 URL。

但是,在执行最终图形请求时,出现错误:

UserInfo={com.facebook.sdk:FBSDKErrorDeveloperMessageKey=Response is a non-text MIME type; endpoints that return images and other binary data should be fetched using NSURLRequest and NSURLSession}

我无法使用 NSURLRequest 或 NSURLSession 来获取 URL。需要明确的是,在 Facebook Graph Explorer 中使用“/”+ id +“/picture”时,我能够获取封面照片 URL。但是,它不适用于 iOS。我相信我在以下方面拥有正确的权限:

        let loginButton = LoginButton(readPermissions: [ .publicProfile, .userPhotos ])

标签: iosswiftfacebook

解决方案


    func findCoverPhotoPicture(id: String) {
    print("find cover url", id)
    print("https://graph.facebook.com/" + id + "/picture")

    let imgURL = "https://graph.facebook.com/" + id + "/picture"
    do {
        let covImg: UIImage = try UIImage(data: Data(contentsOf: URL(string: imgURL)!))!
        self.picView.image = covImg
    } catch {
        print(error)
    }
}

推荐阅读