The API for sending graph requests has been rebuilt from the ground-up for Swift.
You can still send requests in the same way as you do with the existing iOS SDK. The following code example demonstrates how to use the Graph API to get basic profile information for the current user using the previous/classic method.
import FacebookCore
let connection = GraphRequestConnection()
connection.add(GraphRequest(graphPath: “/me”)) { httpResponse, result in
switch result {
case .success(let response):
print(“Graph Request Succeeded: \(response)”)
case .failed(let error):
print(“Graph Request Failed: \(error)”)
}
}
connection.start()
The current approach to Graph API requests uses a custom type-safe API for the requests. This allows you to reason about the types of your graph API requests more accurately, and provides a clean API for consuming this data.
The following code example demonstrates the same Graph API query as the previous example, but does so using the type-safe approach.
import FacebookCore
struct MyProfileRequest: GraphRequestProtocol {
struct Response: GraphResponseProtocol {
init(rawResponse: Any?) {
// Decode JSON from rawResponse into other properties here.
}
}
var graphPath = “/me”
var parameters: [String : Any]? = [“fields”: “id, name”]
var accessToken = AccessToken.current
var httpMethod: GraphRequestHTTPMethod = .GET
var apiVersion: GraphAPIVersion = .defaultVersion
}
let connection = GraphRequestConnection()
connection.add(MyProfileRequest()) { response, result in
switch result {
case .success(let response):
print(“Custom Graph Request Succeeded: \(response)”)
print(“My facebook id is \(response.dictionaryValue?[“id”])”)
print(“My name is \(response.dictionaryValue?[“name”])”)
case .failed(let error):
print(“Custom Graph Request Failed: \(error)”)
}
}
connection.start()