How to share items with a UIActivityViewController

Another option to share items in your app is to bring up a dialogue that contains several options, like the Photos app does:

Screen Shot 2013-12-06 at 18.54.19

This is done with a UIActivityViewController and requires minimal work on our part. This controller is very clever and only brings up relevant services (you can’t print a video, or you can’t share a video to Twitter, so those options don’t appear in the list).

Here’s how we use it:

- (IBAction)activityController:(id)sender {

    // items to share
    UIImage *image = [UIImage imageNamed:@"picture.jpg"];
    NSArray *items =  @[image];

    // create the controller
    UIActivityViewController *controller = [[UIActivityViewController alloc]initWithActivityItems:items applicationActivities:nil];

    [self presentViewController:controller animated:YES completion:nil];
}

UIActivityViewController does not require any frameworks to be imported into your class. The items array can take anything from text, pictures or video (just not mixed together). If an item is not setup the icon won’t appear in the list.

You can specify which items to exclude from the list by defining an array of UIActivities, and you can specify a completion block.





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.