使用 swift 5 的 facebook 图形请求

问题描述 投票:0回答:2

我正在尝试实现 Facebook 图形请求,就像他们的开发人员文档页面上所描述的那样(https://developers.facebook.com/docs/swift/graph)。 Xcode 10.2.1、Swift 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()

有人可以帮忙吗?

ios swift facebook facebook-ios-sdk
2个回答
18
投票

我也有同样的行为。我在 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)

    }

}

0
投票

我对上一个答案进行了改进。如果您请求诸如“location {location {country_code}}”之类的参数,您将得到一个超过 1 级的 json。为此,有必要将结果转换为 [String: Any],因为如果不这样做,您将得到 nil。

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

    if let result = result as? [String:Any],
     let email = result["email"] as? String {
        if let location_data = result["location"] as? [String: Any],
         let location = location_data["location"] as? [String: Any],
         let countryCode = location["country_code"] as? String {
            country = countryCode
        }
     }
}
© www.soinside.com 2019 - 2024. All rights reserved.