iOS 7 Views - Activity ViewController

The UIActivityViewController class is a standard view controller that you can use to offer various services from your application. The system provides several standard services, such as copying items to the pasteboard, posting content to social media sites, sending items via email or SMS, and more. In this tutorial, we will share some text input from a textfield using a activity view controller.

Update: Nov 27, 2014. The rewritten version in Swift for iOS 8 and Xcode 6 is available here.

Open Xcode and create a new Single View Application. For product name, use iOS7ActivityViewControllerTutorial 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.

First, let's create the user interface, Go to the storyboard and drag a text field and a button to the main view. Set the title of the button to share. The storyboard should look like this

Select the Assistant Editor and open ViewController.m. Ctrl and drag from the text field to the @interface section and create the following outlet

Ctrl and drag from the share button to the @interface section and create the following outlet

Next, Ctrl and drag from the same share button to the @interface section and create the following action

The @interface section of ViewController.m should now look like this

@interface ViewController () 

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIButton *shareButton;

- (IBAction)shareText:(UIButton *)sender;


If we would run the project now, there is no way to dismiss the keyboard when editing our textfield. Let's fix this first. Make our viewController comform to the TextFieldDelegate protocol by changing the @interface line to

@interface ViewController () <UITextFieldDelegate>

Next, add the following line add the end of the viewDidLoad method

self.textField.delegate = self;

When the user is in editing mode the keyboard should be dismissed when the user press return. This can be accomplished by creating the textFieldShouldReturn method

- (BOOL)textFieldShouldReturn:(UITextField *)textField
[textField resignFirstResponder];
return YES;

Now it is time to take a look at our activity view controller. First, create the following property in the @interface section 

@property (strong, nonatomic) UIActivityViewController *activityViewController;

Next, implement the shareText method

- (IBAction)shareText:(UIButton *)sender {
self.activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[self.textField.text] applicationActivities:nil];

[self presentViewController:self.activityViewController animated:YES completion:nil];

The activity view controller is initialized with the text of our textfield as an activity item. On the iPhone an activity view controller can only be presented through a modal view. Build and Run, enter some text and press the share button. The activity view controller should be displayed.

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