Extending UIActivityViewController

Tim Johnsen
iOS App Development
3 min readAug 6, 2018

UIActivityViewController, the standard iOS share menu or “share sheet,” was introduced way back in iOS 6. In iOS 8 it was made significantly more valuable to iOS developers with the addition of share extensions and action extensions. Using UIActivityViewController allows developers to tap into the full suite of sharing services available in other apps on their users’ devices.

UIActivityViewController is quite powerful, however customizing the actions it performs can be cumbersome. The UIActivityItemSource protocol that allows you to tailor sharing actions when using UIActivityViewController is limited to customizing only a few properties of the item being shared. Additionally, some products have sharing APIs that are superior to the share extensions they provide through UIActivityViewController. Snapchat, for example, provides a rich sharing SDK for third party developers to use that is more powerful than the share extension its app provides. Many developers have turned to implementing their own bespoke sharing menus because of these drawbacks. These custom sharing menus aren’t ideal because they’re inconsistent and can hide apps that users expect in the standard share menu. I feel that the best practice is to stick to the share menu the system provides, and I’m not alone.

Nonstandard sharing menus from some popular apps

My selfie-a-day app, Close-up, has always been a sort of private photo journal, but I’ve always wanted to provide rich sharing options to my users for when they do choose to share. UIActivityViewController was clearly the best choice for this, but back in 2015 I wanted to customize some of the actions for existing apps that appear in it to provide better experiences. Because of this, I wrote a subclass of UIActivityViewController named TJActivityViewController that allows you to easily override actions by

  • Overriding particular actions or actions that match regexes with blocks.
  • Overriding the items vended to particular actions.

When I wrote Burst earlier this year, this exact component came in handy for sharing once again. Because I’ve found it so useful, today I’m open sourcing TJActivityViewController for others to use! I hope that it enables others to use UIActivityViewController in place of custom sharing menus or enhance the experience of using the standard iOS share sheet. It’s available on GitHub here!

Overriding an activity using TJActivityViewController is as simple as

[activityViewController overrideActivityType:/*An activity type string*/ withBlock:^{
// Your custom override.
}];

And you can also customize the item that’s returned to an activity using

[activityViewController overrideItemForActivityType:/*An activity type string*/ withBlock:^id {
return /*An object to be shared through the activity*/;
}];

I currently use TJActivityViewController in my apps to

  • Enhance sharing to Instagram, Snapchat, Facebook, and Messenger.
  • Send rich HTML emails using MFMailComposeViewController when people share through email.
  • Put band-aids on GIF sharing to Twitter, Tumblr, and iMessage, which treat animated GIFs as still images in some situations.
  • Save photos to a dedicated app-specific album when users choose to save to their device.

I hope you find it useful as well!

--

--

Tim Johnsen
iOS App Development

iOS developer at Retro.app. Previously Patreon, Instagram, Pinterest, Flipboard. Creator of Opener, Close-up, and other apps.