Facebook Graph Request using Swift3 -

Gavin picture Gavin · Sep 25, 2016 · Viewed 14k times · Source

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.

Answer

elp picture elp · Sep 30, 2016

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.