Facebook Graph Request using Swift3 -

Przepisuję moje prośby o Wykresy z najnowszym Swift3. Podążam za poradnikiem znalezionym tutaj - 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()

Jednak dostaję ten błąd w połączeniu.metoda dodania:

Type ViewController.UserProfileRequest.Response does not conform to protocol GraphRequestProtocol.
Nie wiem, co tu zmienić. Wydaje się, że przewodnik programisty nie jest aktualny na Swift3, ale nie jestem pewien, czy to jest problem. Czy ktoś może zobaczyć, co tu jest nie tak? Dzięki.
Author: Gavi, 2016-09-25

4 answers

Przeglądając problemy z githubem, znalazłem rozwiązanie.
https://github.com/facebook/facebook-sdk-swift/issues/63

Dokumentacja Facebook dla Swift 3.0 i SDK 0.2.0 nie jest jeszcze aktualizowana.

To działa dla mnie:

    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"])
            }
        }
    }
Smacznego.
 48
Author: elp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-30 11:38:06

Ten kod działa dla mnie, najpierw robię login z poprawnymi uprawnieniami, a następnie buduję GraphRequest dla uzyskania informacji o użytkowniku.

let login: FBSDKLoginManager = FBSDKLoginManager()
    // Make login and request permissions
    login.logIn(withReadPermissions: ["email", "public_profile"], from: self, handler: {(result, error) -> Void in

        if error != nil {
            // Handle Error
            NSLog("Process error")
        } else if (result?.isCancelled)! {
            // If process is cancel
            NSLog("Cancelled")
        }
        else {
            // Parameters for Graph Request without image
            let parameters = ["fields": "email, name"]
            // Parameters for Graph Request with image
            let parameters = ["fields": "email, name, picture.type(large)"]

            FBSDKGraphRequest(graphPath: "me", parameters: parameters).start {(connection, result, error) -> Void in
                if error != nil {
                    NSLog(error.debugDescription)
                    return
                }

                // Result
                print("Result: \(result)")

                // Handle vars
                if let result = result as? [String:String],
                    let email: String = result["email"], 
                    let fbId: String = result["id"], 
                    let name: String = result["name"] as? String, 
                    // Add this lines for get image
                    let picture: NSDictionary = result["picture"] as? NSDictionary,
                    let data: NSDictionary = picture["data"] as? NSDictionary,
                    let url: String = data["url"] as? String {

                    print("Email: \(email)")
                    print("fbID: \(fbId)")
                    print("Name: \(name)")
                    print("URL Picture: \(url)")
                }
            }
        }
    })
 8
Author: Santiago Restrepo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-07 17:06:00

Oto Mój kod jak. Używam Xcode 8, Swift 3 i działa dobrze dla mnie.

let parameters = ["fields": "email, id, name"]
            let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: parameters)

            _ = graphRequest?.start { [weak self] connection, result, error in
                // If something went wrong, we're logged out
                if (error != nil) {
                    // Clear email, but ignore error for now
                    return
                }

                // Transform to dictionary first
                if let result = result as? [String: Any] {
                    // Got the email; send it to Lucid's server
                    guard let email = result["email"] as? String else {
                        // No email? Fail the login
                        return
                    }
                    guard let username = result["name"] as? String else {
                        // No username? Fail the login
                        return
                    }

                    guard let userId = result["id"] as? String else {
                        // No userId? Fail the login
                        return
                    }                       
                }
            } // End of graph request
 5
Author: Cheng Yang Chen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-14 22:36:44

Twoje UserProfileRequest powinno wyglądać tak:

fileprivate struct UserProfileRequest: GraphResponseProtocol {
  fileprivate let rawResponse: Any?

  public init(rawResponse: Any?) {
    self.rawResponse = rawResponse
  }

  public var dictionaryValue: [String : Any]? {
    return rawResponse as? [String : Any]
  }

  public var arrayValue: [Any]? {
    return rawResponse as? [Any]
  }

  public var stringValue: String? {
    return rawResponse as? String
  }
}
 2
Author: Ivo Stoyanov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-27 13:57:29