我正在尝试使用以下代码从Facebook页面读取帖子:

class FacebookGraphAPI {
    class func getPosts(fromPageWithID pageId: String, parameters: [String: AnyObject]?, completion: @escaping ([FacebookPost]?, Error?) -> Void) {
        self.getAccessToken { (accessToken, error) in
            if error != nil {
                completion(nil, error)
                return
            }

            let params = ["access_token": accessToken, "fields": "created_time,message,story"]

            if let request = FBSDKGraphRequest(graphPath: "\(pageId)/posts", parameters: params) {
                request.start { (connection, result, error) in
                    if error != nil {
                        completion(nil, error)
                        return
                    }

                    guard let resultDict = result as? [String: AnyObject],
                        let data = resultDict["data"] as? NSArray
                        else {
                        completion(nil, nil)
                        return
                    }

                    var posts = [FacebookPost]()

                    for item in data {
                        posts.append(FacebookPost(dict: item as! NSDictionary))
                    }

                    completion(posts, nil)
                }
            }

            completion(nil, nil)
        }
    }

    class func getAccessToken(completion: @escaping (String?, Error?) -> Void) {

        let clientId = Bundle.main.object(forInfoDictionaryKey: "FacebookAppID") as! String
        let clientSecret = Bundle.main.object(forInfoDictionaryKey: "FacebookAppSecret") as! String

        let params = ["client_id": clientId, "client_secret": clientSecret, "grant_type": "client_credentials", "fields": "access_token"]

        if let request = FBSDKGraphRequest(graphPath: "oauth/access_token", parameters: params) {
            request.start(completionHandler: { (connection, result, error) in
                if error != nil {
                    completion(nil, error)
                    return
                }

                guard let resultDict = result as? [String: AnyObject] else {
                    completion(nil, nil)
                    return
                }

                let accessToken = resultDict["access_token"] as! String
                completion(accessToken, nil)
            })
        }
    }
}


然后我使用例如下列:

FacebookGraphAPI.getPosts(fromPageWithID: "{page-id}", parameters: ["limit": 5 as AnyObject]) { (posts, error) in
    guard error == nil else { return }

    ...
}


我得到的错误是:在第二个FBSDKGraphRequest启动时-[_SwiftValue length]: unrecognized selector sent to instance

我尝试删除第一个FBSDKGraphRequest,然后至少在completionHandler中得到响应。几乎就像我不能做多个FBSDKGraphRequest一样。

任何帮助是极大的赞赏。

提前致谢!

最佳答案

在考虑到错误并记住FBSDKCoreKit框架是用Objective-C编写时,我终于设法找到了问题。

我需要做的就是将参数数组内的accessToken强制转换为NSString

我更改了以下内容:

let params = ["access_token": accessToken, "fields": "created_time,message,story"]


至:

let params = ["access_token": accessToken! as NSString, "fields": "created_time,message,story"]

关于ios - 无法创建多个FBSDKGraphRequest,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40126132/

10-10 09:57