iOS Share extension — Swift 5.1

Share contents into your app in a few easy steps.

Fabio Pelizzola
Apr 12, 2020 · 5 min read
Photo by Yura Fresh on Unsplash

“How the `hack` is Share Extension? How can I share contents into my app?” If you are wondering about one of these questions you are in the right place!

Share Extension is an easy way that Apple provides to share contents (images, audio, files, etc.) from one app to another, even made by different developers. You just need to follow a few simple steps and if you don’t know what’s an extension in the iOS world, you can just have a quick look here.

To follow this article it should be better to know what target and schemes are in Xcode and how they work but it’s not mandatory.

1. Add a Share Extension to your project

Go into the project section, click on the + button and select the Share extension from the list. Just call it Share or whatever name you prefer.

The system asks you if you want to activate the Share scheme , just select Activate. This is required because XCode creates a new Target with an associated scheme for the extension. You can see the new target in the target list.

At this step, you can try to run the app (be sure to run your main target and not the share one) and try to share a photo from the camera roll. Your app should appear in the share view list, amazing!

I hope you liked this article, enjoy your super cool app that can now import any kind of file without writing any line of code!

Ops…wait a moment… Is it so easy? Of course not.

2. The NSExtensionActivationRule

What’s happening under the hood?

When you try to share a file from an app the system will look for all the app that can handle that kind file (if it’s an image it will look for all apps that can import images). Then, if you click on your app’s icon from the share view the system will lunch your Share target that will handle and process the file.

We wanna be sure that our app appears in the list only if the file that the user is trying to share can be handled by our app. To do this we have to change the NSExtensionActivationRule in the info.plist.

Be sure to select the info.plist from the share target and not from the main one. The default is TRUEPREDICATE, this means that your app is saying:

 Hey, I’m the best, I can handle any kind of file! Just Try! 

but of course, that’s not true.

Change NSExtensionActivateRule type from String into Dictionary and then add NSExtensionActivationSupportsImageWithMaxCount with value 1.

Now your app is saying:

Hey I can handle images, but just once at a time!

This means that if you don’t select an image or you select more than one, the extension app automatically hides from share-sheet.

Instead of using a dictionary as activation rule you can keep the string and replace the TRUEPREDICATE with more complex queries like this one:

SUBQUERY($extensionItem.attachments, $attachment, 
ANY $attachment.registeredTypeIdentifiers
UTI-CONFORMS-TO "public.image").@count >= 1

If you wanna know more about activation rules and syntax, just read the documentation here.

In the project list, you will also find a field name NSExtensionPrincipalClass. This is the VC that will be invoked when you share the file. You can also use storyboard if you prefer. This is up to you.

3. Handle the shared content

Let’s write some code know! Open your ShareViewController.swift file that Xcode created for you when you have created the target.

You can see that your VC conforms to SLComposeServiceViewController. Let’s take a look at the documentation:

A view controller that you present from your share app extension, allowing the user to compose social media posts.

This VC provides a standard UI and different callbacks that you can use to handle the content. This UI is super cool but it’s more suitable for social apps and probable you want to create your custom UI, so I’ll make this assumption for the following lines, but the process is pretty much the same.

So, delete all lines that XCode already provides for you and replace them with an empty view controller and add on top the tag @objc(ShareExtensionViewController).

Now let’s complete the handleSharedFile() function:

We can access the attachments through the extensionContext. Then for each attachement we check that the content type is the same that we expected (for images it’s a kTTypeData and then we can cast it to an URL).

Then we load the item and in the callback we can save it. In the example I use UserDefaults but you can save it on disk or whenever you prefer.

In a real app should be better to check the error and change UI accordingly. Also, don’t forget to add some UI (from the storyboard or from the code) otherwise when you share the file your view controller will be transparent.

It’s done! You share the file into your app and now you can use it! Amazing!

4. Debug

If you need to debug the ShareViewController you need to:

  • Make sure that Product -> Scheme -> Edit Scheme -> Executable is set to Ask on Launch .
  • Run the Target (Share-Debug)
  • Choose from the list an app that can share the content that you wanna test (for example Photos for images or Voicesfor audio.

I hope you enjoy this article! Leave a clap if you like it and for any question just ping me, I’ll be happy to help you :)

Mac O’Clock

The best stories for Apple owners and enthusiasts

Fabio Pelizzola

Written by

Hi! I’m Fabio, iOS Engineer from Italy with a passion for travelling and playing music.

Mac O’Clock

The best stories for Apple owners and enthusiasts

Fabio Pelizzola

Written by

Hi! I’m Fabio, iOS Engineer from Italy with a passion for travelling and playing music.

Mac O’Clock

The best stories for Apple owners and enthusiasts

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store