Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

facebook graph request using swift 5

I am trying to implement a facebook graph request just as described on their developer´s doc page (https://developers.facebook.com/docs/swift/graph). Xcode 10.2.1, swift 5.

But I keep getting the following error:

Contextual closure type '(GraphRequestConnection?, Any?, Error?) -> Void' expects 3 arguments, but 2 were used in closure body

I have done a lot of research and tried to fill in several arguments but I simply cannot figure out what the missing third argument might be.

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()

Can someone help please?

like image 609
Franz Avatar asked May 20 '19 10:05

Franz


People also ask

Is Facebook Graph API deprecated?

API Version Deprecations: As part of Facebook's Graph API and Marketing API, please note the upcoming deprecations: August 3, 2021: Graph API v3. 3 will be deprecated and removed from the platform. August 25, 2021 Marketing API v9.

How do I get my Facebook Graph API user ID?

The simplest way to get a copy of the User Profile object is to access the /me endpoint: FB. api('/me', function(response) { }); This will this will return the users name and an ID by default.


1 Answers

I got the same behavior. I am not that experienced in Swift and the Facebook Graph API and I'm not sure if it is a good solution but, currently (until I find a better solution), for me works:

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)

    }

}
like image 63
Sebastian Fox Avatar answered Oct 09 '22 21:10

Sebastian Fox