Use the Graph API to get data in and out of Facebook’s social graph.
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.
Prerequisites
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’s email address, your app must be authorized for the email permission. Be sure you are familiar with:
- Login on iOS
- Managing Permissions on iOS
- Handling Errors
You also need your development environment set up for the iOS SDK and your app set up.
Fetch User Data
The SDK has two classes to work with the Graph API: FBSDKGraphRequest and FBSDKGraphRequestConnection which are similar to the Foundation framework’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:
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@”me” parameters:nil]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@”fetched user:%@”, result);
}
}];
}
Posting Data
The FBSDKRequest class provides other initializers to specify parameters and the HTTP method. For example if you want to post a status update, this requires publish_actions permissions, then you make this call:
if ([[FBSDKAccessToken currentAccessToken] hasGranted:@”publish_actions”]) {
[[[FBSDKGraphRequest alloc]initWithGraphPath:@”me/feed”
parameters: @{ @”message” : @”hello world”}
HTTPMethod:@”POST”]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@”Post id:%@”, result[@”id”]);
}
}];
}
Batch Requests
In Graph API you can make batch requests in a single HTTP request, see Graph API, Making Batch 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’s Likes:
if ([[FBSDKAccessToken currentAccessToken] hasGranted:@”user_likes”]) {
FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]
initWithGraphPath:@”me” parameters:nil];
FBSDKGraphRequest *requestLikes = [[FBSDKGraphRequest alloc]
initWithGraphPath:@”me/likes” parameters:nil];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:requestMecompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
//TODO: process me information
}];
[connection addRequest:requestLikescompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
//TODO: process like information
}];
[connection start];}
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
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:
// Assuming you have a UIImage reference
UIImage *someImage = …;
FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
content.photos = @[[FBSDKSharePhoto photoWithImage:someImage userGenerated:YES] ];
// Assuming self implements <FBSDKSharingDelegate>
[FBSDKShareAPI shareWithContent:content delegate:self];You can also share videos using the FBSDKShareVideoContent type.
Delete Objects
You can also delete objects that your app created by sending a DELETE request with the object’s ID as the graph path.
For example, image you to publish a post as shown above and received an ID of β1234β. The following code would delete the post:
if ([[FBSDKAccessToken currentAccessToken] hasGranted:@”publish_actions”]) {
[[[FBSDKGraphRequest alloc]initWithGraphPath:@”1234″
parameters:nil
HTTPMethod:@”DELETE”]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@”Deleted postβ);
}
}];
}