avatarFabio Pelizzola

Summary

This article provides a step-by-step guide on how to create a Share Extension in an iOS app using Swift 5.1, allowing users to share content from one app to another.

Abstract

The article begins by explaining what a Share Extension is and its purpose in iOS development. It then guides the reader through the process of adding a Share Extension to their project in Xcode, demonstrating how to create a new target and activate the Share scheme. The article also covers the NSExtensionActivationRule, which determines when the app should appear in the share view list based on the type of file being shared. The author provides code examples for handling shared content and debugging the ShareViewController.

Opinions

  • The author believes that Share Extension is an easy way for developers to allow users to share content between apps.
  • The author emphasizes the importance of understanding what target and schemes are in Xcode and how they work.
  • The author suggests that it is not enough to simply add a Share Extension to an app, but developers must also ensure that their app only appears in the share view list when appropriate.
  • The author recommends using a custom UI for the ShareViewController instead of the standard UI provided by SLComposeServiceViewController.
  • The author encourages developers to add some UI to the ShareViewController to avoid a transparent view controller when sharing a file.
  • The author provides a detailed guide on how to debug the ShareViewController.
  • The author concludes by expressing their hope that the reader enjoys the article and invites them to leave a clap if they like it.

iOS Share extension — Swift 5.1

Share contents into your app in a few easy steps.

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 :)

Swift
iOS
Xcode
Share
Programming
Recommended from ReadMedium