Get Facebook User Data in Your iOS App

You can get user data by using the Facebook Graph API if the user has given their permission to share their data and your app has the appropriate permissions to receive this data. This topic shows you how to make a single request for data and how to have a batch of requests in a single request.

Before You Begin

You will need to integrate the FBSDKCoreKit framework from the Facebook SDK for iOS into your app. Learn more.

Add the following import statement:

import FBSDKCoreKit

Single Request

To make a single request, use the GraphRequest class to encapsulate the components of the request and the GraphRequestConnection class to issue the request. Before you make the request, make sure that you have a valid access token.

The following code example demonstrates how to make a single request:

if AccessToken.current != nil {
    GraphRequest(graphPath: "me").start { connection, result, error in
        if let result = result {
            print("Fetched Result: \(result)")
        }
    }
}

Multiple Requests

You can make multiple requests for data from a single request by using GraphRequestConnection. By putting all of your requests into a single request, you make minimize the network traffic.

The following code example demonstrates how to make a request with multiple requests for likes from users who have given permission to share that data.

if AccessToken.current?.hasGranted("user_likes") == true {
    let requestMe = GraphRequest(graphPath: "me")
    let requestLikes = GraphRequest(graphPath: "me/likes")

    let connection = GraphRequestConnection()
    connection.add(requestMe) { connection, result, error in
        // Process me result
    }

    connection.add(requestLikes) { connection, result, error in
        // Process like result
    }

    connection.start()
}

You can also specify batch parameters with the addRequest(completion:) overloads, which includes the ability to create a batch with dependent requests.