首页 > 解决方案 > 使用 swift 5 的 facebook 图形请求

问题描述

我正在尝试按照他们的开发人员文档页面(https://developers.facebook.com/docs/swift/graph)上的描述实现一个 facebook 图形请求。Xcode 10.2.1,斯威夫特 5。

但我不断收到以下错误:

上下文闭包类型 '(GraphRequestConnection?, Any?, Error?) -> Void' 需要 3 个参数,但在闭包体中使用了 2 个

我做了很多研究并试图填写几个论点,但我根本无法弄清楚缺少的第三个论点可能是什么。

import FBSDKCoreKit

let connection = GraphRequestConnection()
connection.add(GraphRequest(graphPath: "/me")) { httpResponse, result in
    switch result {
        case .success(let response):
        print ("Graph Request succeeded: \(resonse)")

        case .failed (let error):
            print ("Graph Request failed: \(error)")
    }
}
connection.start()

有人可以帮忙吗?

标签: iosswiftfacebookgraphsdk

解决方案


我有同样的行为。我在 Swift 和 Facebook Graph API 方面经验不足,我不确定这是否是一个好的解决方案,但目前(直到我找到更好的解决方案)对我来说是有效的:

connection.add(GraphRequest(graphPath: "/me", parameters: ["fields":"email"])) { httpResponse, result, error   in
    if error != nil {
        NSLog(error.debugDescription)
        return
    }

    // Handle vars
    if let result = result as? [String:String],
        let email: String = result["email"],
        let fbId: String = result["id"] {

        // internal usage of the email
        self.userService.loginWithFacebookMail(facebookMail: email)

    }

}

推荐阅读