UIActivityViewController Sharing Content on iOS 6 – Part 1 – Share With Facebook, Twitter, Messages, Mail, Weibo…

With iOS 6 Apple introduced UIActivityViewController, a view controller to share content with various services (internal to the OS and otherwise). Services range from sending an SMS or email, to copying content to the pasteboard (all iOS internal services) to posting information to Twitter, Facebook and Weibo (the Chinese version of Twitter).

This post will show you how to send content to the various iOS specific services via the UIActivityViewController view controller. I’ll also show how to tailor the list of services that are displayed in the view controller.

Create UIActivityViewController

To display a simple activity view controller:

// Create the content
NSString *message = @"The Farm";
UIImage *image = [UIImage imageNamed:@"Farm.jpg"];
NSArray *arrayOfActivityItems = [NSArray arrayWithObjects:message, image, nil];
 
// Display the view controller
UIActivityViewController *activityVC = [[[UIActivityViewController alloc]
  initWithActivityItems: arrayOfActivityItems applicationActivities:nil] autorelease];

The view controller looks as follows on the simulator (I have no idea why the Messages application does not have an icon).

If you prefer to tailor the list of services shown, you can specify an array of strings, where each string is an activityType representing a service you don’t care to show in the view controller:

// Tailor the list of services displayed
activityVC.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeMessage,
    UIActivityTypeSaveToCameraRoll, UIActivityTypePrint, UIActivityTypePostToWeibo, 
    UIActivityTypeCopyToPasteboard];

With this change, the view controller will now look as follows:

In a future post I’ll cover how to use the UIActivity class to develop your own application-specific services. The final post in this series will walk through completion handlers, demonstrating how to write code that is executed after a service is completed to inform your application of the outcome of the service request.

In Part 2 I’ll show how to create an activity to support a custom service.

Comments are closed.