iOS Share extension — Swift 5.1
Share contents into your app in a few easy steps.
“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).