Social Sharing in iOS 6 : Post to Facebook

iOS 6 introduces a new framework known as the Social Framework.  The Social framework lets you integrate your app with any supported social networking services. In  this tutorial we use the social framework to send a post to Facebook.

Update: Sep 29, 2014. The rewritten tutorial in Swift for iOS 8 and Xcode 6 is available here.

Open Xcode and create a new Single View Application. For product name, use FacebookPostDemo and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices, and that the Use Storyboards checkbox is deselected and Use Automatic Reference Counting checkbox is selected.

First, Let's create our User Interface. Go to ViewController.xib. Drag a Button to the main View. Double-click the Button and enter the text "Post to Facebook". The main View should look like this.

In ViewController.m in the interface section declare the following IBAction method

@interface ViewController ()

- (IBAction)postToFacebook:(id)sender;


Go back to ViewController.xib. Select the Assistant Editor. On the right side select ViewController.m. Ctrl-click and drag from the Button to the IBAction method postToFaceBook in ViewController.m.

Next, implement the postToFacebook method

- (IBAction)postToFacebook:(id)sender
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

[controller setInitialText:@"Testing Posting to Facebook"];
[self presentViewController:controller animated:YES completion:nil];

The SLComposeViewController presents a view to compose a post to the approperiate social network. We initialize a SLCompozeViewController with a initial text, which we'll present. As you can see, all functionality is available from inside this controller.

Build and Run, and start posting to Facebook.

You can download the source code of the FacebookPostDemo at the ioscreator repository on github.