Social Sharing in iOS 6 : The Activity Viewcontroller

Integration of social networks into iOS 6 applications is performed through the use of the UIActivityViewController class. Social networks supported by these classes consist of Facebook, Twitter and Sina Weibo, though it is probable that more will follow in future SDK releases. When using the UIActivityViewController class, the user is presented with a screen providing a choice of social network services to which a message may be posted. Once the message has been posted, the class handles all aspects of connecting to the user’s chosen social network and subsequently posting the update to that service.

This tutorial targets iOS6 and earlier. Click here for the iOS7 version of the activity view controller tutorial.

Open Xcode and create a new Single View Application. For product name, use SocialSharingDemo 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 is deselected and Use Automatic Reference Counting checkboxes is selected (Unit Tests will not be necessary for this project).

First, create The User Interface. Select the ViewController.xib and drag a TextField and a Button to the View. Select the TextField and go to the Attributes Inspector. Enter the following text for the placeholder "Insert text to share". Double-click the Button and enter "Share". The View should look like this.

Next, create the IBOutlet properties and IBActions. Go to ViewController.m and insert the following lines in the @interface section.

@interface ViewController ()

@property (nonatomic, strong) IBOutlet UITextField *textField;
@property (nonatomic, strong) IBOutlet UIButton *shareButton;
@property (nonatomic, strong) UIActivityViewController *activityViewController;

- (IBAction)shareButtonPressed:(id)sender;


We create a property for the Activity ViewController, so it can be initialized and viewed later on.

Next, connect the IBOUtlets and IBActions in the Interface Builder. Go to ViewController.xib and make the following connections:

  • TextField -> textField
  • Button -> shareButton
  • Button -> shareButtonPressed

Next, we fill in the shareButtonPressed method. Go to ViewController.m and create the method in the @implementation section

- (IBAction)shareButtonPressed:(id)sender 
self.activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[self.textField.text] applicationActivities:nil];
[self presentViewController:self.activityViewController animated:YES completion:nil];

The Activity ViewController is initialized and the text of our textField is passed on. Then the Activity View is presented modally.

Build and Run, enter some text to share and press the share button. Now you can choose the social network where you want to share the message.

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