# Using the Graph API, iOS



Use the [Graph API](https://developers.facebook.com/docs/graph-api/quickstart) to get data in and out of Facebook&#039;s [social graph](https://developers.facebook.com/docs/opengraph).

This includes:

- Fetching profile information to provide social context.
- Fetching user information such as their likes or photos.
- Publish posts (including videos or photos) to Facebook.
- Publishing open graph stories to Facebook.
- For more features, see the [Graph API documentation](https://developers.facebook.com/docs/graph-api).

This guide describes how you work with the Graph API using the Facebook SDK for iOS.

## Prerequisites &#123;#prerequisites&#125;

Calling the Graph API requires someone to login to your app via Facebook and authorize permissions for your app.

For example, if you want to fetch someone&#039;s email address, your app must be authorized for the `email` permission. Be sure you are familiar with:

- [Login on iOS](https://developers.facebook.com/documentation/facebook-login/ios)
- [Managing Permissions on iOS](https://developers.facebook.com/documentation/facebook-login/ios/permissions)
- [Handling Errors](https://developers.facebook.com/documentation/ios/errors)

You also need your development environment set up for the iOS SDK and your app set up, see [iOS, Getting Started Guide](https://developers.facebook.com/documentation/ios/getting-started).

## Fetch User Data &#123;#fetching&#125;

The SDK has two classes to work with the Graph API: `FBSDKGraphRequest` and `FBSDKGraphRequestConnection` which are similar to the Foundation framework&#039;s `NSURLRequest` and `NSURLRequestConnection`.

To use the `FBSDKGraphRequest` you provide the request with a specific Graph API endpoint. Then call `FBSDKGraphRequestConnection` to start the request and process its completion.

For convenience, the SDK has a `startWithCompletionHandler:` method on `FBSDKGraphRequest` to implicitly create a `FBSDKGraphRequestConnection` for you.

For example to fetch the profile information for the person currently logged into your app, make this call:

```
guard AccessToken.current != nil else &#123; return &#125;

let request = GraphRequest(graphPath: &quot;me&quot;, parameters: [:])
request.start() &#123; connection, result, error in
    if let result = result, error == nil &#123;
        print(&quot;fetched user: \(result)&quot;)
    &#125;
&#125;
```

## Batch Requests &#123;#batching&#125;

In Graph API you can make batch requests in a single HTTP request, see [Graph API, Making Batch Requests](https://developers.facebook.com/docs/graph-api/making-multiple-requests).

With the SDK, you can construct multiple requests and add them to the same `FBSDKGraphRequestConnection` instance. You should batch requests whenever possible to minimize network traffic.

For example here we request someone&#039;s Likes:

```
guard let token = AccessToken.current else &#123; return &#125;

if token.hasGranted(&quot;user_likes&quot;) &#123;
    let meRequest = GraphRequest(graphPath: &quot;me&quot;, parameters: [:])
    let likesRequest = GraphRequest(graphPath: &quot;me/likes&quot;, parameters: [:])

    let connection = GraphRequestConnection()
    connection.add(meRequest) &#123; connection, result, error in
        // Process the &#039;me&#039; information
    &#125;
    connection.add(likesRequest) &#123; connection, result, error in
        // Process the &#039;likes&#039; information
    &#125;

    connection.start()
&#125;
```

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

## Share Photos and Videos &#123;#photos_and_videos&#125;

While you can always manually create `FBSDKGraphRequests` to work with the Graph API endpoints, the Facebook SDK for iOS simplifies sharing photos and videos with `FBSDKShareKit.framework`. For example, to share a photo:

```
let image: UIImage!
let content = SharePhotoContent()
content.photos = SharePhoto(image: image, isUserGenerated: true)

// Assuming self implements SharingDelegate
let dialog = ShareDialog(
    viewController: self,
    content: content,
    delegate: self
)
dialog.show()
```

You can also share videos using the `FBSDKShareVideoContent` type.  See the [Sharing on iOS](https://developers.facebook.com/documentation/sharing/ios).

## Delete Objects &#123;#deleting&#125;

You can also delete objects that your app created by sending a `DELETE` request with the object&#039;s ID as the graph path.

For example, imagine you published a photo to a Page and received an ID of “1234”. The following code would delete the photo:

```
guard let token = AccessToken.current else &#123; return &#125;

if token.hasGranted(&quot;pages_manage_posts&quot;) &#123;
    let request = GraphRequest(graphPath: &quot;1234&quot;, parameters: [:], httpMethod: .delete)
    request.start &#123; connection, result, error in
        if error == nil &#123;
            print(&quot;Deleted photo&quot;)
        &#125;
    &#125;
&#125;
```

## Debugging Tips &#123;#debugging_tips&#125;

You can experiment and test your requests with the [Graph API Explorer](https://developers.facebook.com/tools/explorer).

You can also enable Graph API debug mode, see [iOS SDK Troubleshooting, Graph API Debug Mode](https://developers.facebook.com/documentation/ios/troubleshooting#graphapidebugmode).
