Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Graph Request using Swift3 -

I am rewriting my graph requests with the latest Swift3. I am following the guide found here - https://developers.facebook.com/docs/swift/graph.

fileprivate struct UserProfileRequest: GraphRequestProtocol {
    struct Response: GraphResponseProtocol {
        init(rawResponse: Any?) {
            // Decode JSON into other properties

        }
    }

    let graphPath: String = "me"
    let parameters: [String: Any]? = ["fields": "email"]
    let accessToken: AccessToken? = AccessToken.current
    let httpMethod: GraphRequestHTTPMethod = .GET
    let apiVersion: GraphAPIVersion = .defaultVersion
}


fileprivate func returnUserData() {


    let connection = GraphRequestConnection()
    connection.add(UserProfileRequest()) {
        (response: HTTPURLResponse?, result: GraphRequestResult<UserProfileRequest.Response>) in
        // Process
    }
    connection.start()

However, I am getting this error in the connection.add method:

Type ViewController.UserProfileRequest.Response does not conform to protocol GraphRequestProtocol.

I can't seem to figure this out what to change here. It seems like the developer guide is not up to date on Swift3, but I am not sure that is the issue.

Is anyone able to see what is wrong here?

Thanks.

like image 884
Gavin Avatar asked Sep 25 '16 05:09

Gavin


People also ask

What data can I get from Facebook Graph API?

The Graph API is the primary way to get data into and out of the Facebook platform. It's an HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.

What is Graph API Facebook?

A Facebook Graph API is a programming tool designed to support more access to conventions on the Facebook social media platform. The core of Facebook's platform is something called the "social graph," which is the element responsible for facilitating all of the online relationships between people, places, things, etc.


1 Answers

Browsing on the github issues, i found a solution.
https://github.com/facebook/facebook-sdk-swift/issues/63

Facebook documentation for Swift 3.0 and SDK 0.2.0 is not yet updated.

This works for me:

    let params = ["fields" : "email, name"]
    let graphRequest = GraphRequest(graphPath: "me", parameters: params)
    graphRequest.start {
        (urlResponse, requestResult) in

        switch requestResult {
        case .failed(let error):
            print("error in graph request:", error)
            break
        case .success(let graphResponse):
            if let responseDictionary = graphResponse.dictionaryValue {
                print(responseDictionary)

                print(responseDictionary["name"])
                print(responseDictionary["email"])
            }
        }
    }

enjoy.

like image 52
elp Avatar answered Sep 18 '22 17:09

elp