How to share items with a UIActivityViewController

- by

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.



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.