iOS

Building a Simple Share Extension in iOS 8 App


Share extensions, introduced in iOS 8, give users an easy and convenient way to share content with other entities, such as social sharing websites or upload services. Previously, sharing content usually entailed switching from one app to another, for example, while surfing in Safari, if you wanted to share a URL, you would copy it, switch to the app you wanted to save or share it in, perform the action and then resume surfing in Safari. With share extensions, users will now be able to share content to your service direct from within the app they are using, be it Safari, Photos or other apps. This isn’t limited to system applications. Any custom application that presents an instance of the UIActivityViewController class will be able to see your sharing extension if you built your extension so that it can handle the file type provided by that application.

We are going to build a Share extension that shares photos to a social networking site. To make things simple we’ll use Imgur for this as it allows users to upload images anonymously (without the images being linked to an account).

Share Extension

Just like any other extension, a share extension cannot be a stand alone app; it must come bundled with a container app. I’ve created a starter project that will be our container app. Download it to follow along.

How the Demo App Works

The app is a simple application called ImgurShare that the user can use to upload images to Imgur. I’ll give the instructions of getting it ready to run shortly, but first let me give an overview of the app.

Its root view is a table view that lists the titles we give to the images uploaded to Imgur.

Share Extension

To upload an image, tap the + button in the navigation bar and you will be presented with a view which enables you to select an image, enter a title for the image and upload to Imgur.

Share Extension #2

After uploading the image, when you go back to the table view, the title of the image uploaded will be listed. When you select an item on the table view, you will be shown the image uploaded and its link on Imgur. To keep it simple, I did not include edit and delete functionality.

Share Extension #3

There is a Copy URL button on the navigation bar of this view, which you can use to copy the image’s URL to the clipboard. You can test it out by copying the URL, opening Safari and pasting in the link. You will see your uploaded image on Imgur.

Share Extension #4

Getting Started

With the app overview out of the way, we can now set it up. There are a couple things you need to do to get it running. First, you’ll need a Client ID from Imgur to use their API and second, you’ll need to set up an App Group. You need a developer account to enable App Groups. In order for an extension to share data with its container app, you need to configure an App Group. This will allow access to a shared container between the extension and container app.

We want the extension to have access to the shared container for two reasons: first, just like the container app, every image uploaded via the extension will be saved and made available for viewing via the container app and second, we use a background session for the upload which requires the image to be saved first before uploading, so we save a temporary image in the shared container which is used in the background upload task. We use background upload because users tend to get back to the host app immediately after finishing their task in extensions, so if the upload is potentially lengthy, we want it to run to completion even when the extension is terminated.

To get the Imgur client id, login to imgur.com or register an account at imgur.com/register. Once logged in, click your username at the top right of the screen and select Settings.

Share Extension #5

Then select Applications from the menu on the right.

Share Extension #6

Select create your own under Apps Used.

image07

Click Register an Application from the menu on the left and select register their application as shown.

image08

On the application form, enter whatever you want for the Application name. For Authorization type, select OAuth 2 authorization without a callback URL, enter your email address in Email and enter whatever you like for description. Enter the security text into the box and submit the form.

image09

After a successful submission, you will be shown your Client ID and Client secret.

image10

Copy the Client ID and open UploadImageService.swift (find it in the ImgurKit group) and paste it in the statement shown.

To configure the App Group, first change the app’s Bundle Identifier. Select ImgurShare in the Project Navigator, and then select the ImgurShare target from the targets list. On the General tab, change the Bundle identifier from com.appcoda.ImgurShare to something else. You need a different identifier because the identifier that you use for your app group has to match the Bundle identifier and it has to be unique.

image11

Next go to the Capabilities tab and switch on the App Groups switch. Add a new group and name it group.com.[DOMAIN_CHOSEN_IN_PREVIOUS_INSTRUCTION].ImgurShare. For my case, I have group.com.appcoda.ImgurShare.

image12

Creating the Share Extension

Open AddImageViewController.swift file and in the shareImage(imageTitle:, imageToUpload:) function, change the container identifier to match the group you created.

Open ImageService.swift and do the same for the following statement in the tempContainerURL(image:, name:) function.

Do the same for the statement shown in the getFileUrl() function in the same class.

You can now run and test the app.

Next we’ll create the Share extension. Select the ImgurShare project in the Project Navigator and then go to Editor > Add Target > iOS > Application Extension > Share Extension

image13

On the next screen set the product name to ImgurUpload and leave the remaining fields set to their default. Click on Finish and activate the ImgurUpload scheme

image14

Next, configure the App Group for the ImgurUpload target. Select the ImgurShare project in the Project Navigator and then select the ImgurUpload target. On the Capabilities tab, turn on the App Groups switch and select the group you created previously for the ImgurShare target.

image15

In order to enable code sharing between the extension and container app, I put the code in a framework. We need to link this to the ImgurUpload target.

With the ImgurUpload target selected, on the General tab, click the + button under the Linked Frameworks and Libraries section. Select ImgurKit.framework from the list and click Add.

image16

With that set up, let’s now look at the files generated when we created the extension. Expand the ImgurUpload group and you will see a storyboard file, an entitlements file, a view controller and a plist file under the Supporting Files group. The entitlements file in an extension, just as in the container app, is generated when you configure App Groups. It holds details of your app group. Just as in Action Extensions, you can use a JavaScript file to get content from web pages in Safari. We won’t be using this in our extension, but for an idea on how to utilize JavaScript files to get data from the host app to the extension’s view controller, you can refer to the previous article we did on Action Extensions. The process and set up is quite similar.

You can use the storyboard file to create a custom interface (or do it in code), but Share extensions come with a default compose view which we’ll use. It’s interface is similar to the compose view you get when you share something to Twitter or Facebook.

We need to specify the type of content that our extension supports by setting its activation rule in the plist file. Open Info.plist and expand the NSExtension key. Then expand the NSExtensionAttributes key to get to the NSExtensionActivationRule. By default, this is set to TRUEPREDICATE which means that your extension will always be made available when the user wants to share content. You need to change this and make it specific if you want your app to be approved by the App Store. Change its type to Dictionary and add a new key-value pair. Set the key name to NSExtensionActivationSupportsImageWithMaxCount, type to Number and value to 1. This specifies that the extension supports sharing a single image at a time.

image17

Looking at the ShareViewController class, you will find the following method stubs.

  • isContentValid() – This is where user input is validated. You validate the text input as well as the content to be shared and return true if validation passes. The Post button on the compose view will remain disabled until true is returned.
  • didSelectPost() – This is called after the user taps the Post button and it is here that you upload the content being shared. Once the upload is scheduled, you must call completeRequestReturningItems([], completionHandler:) so that the host app can un-block its UI. When the upload request is done, it calls the completion handler that was passed into the previous mentioned function call.
  • configurationItems() – The default compose view of the share extension allows you to set what appears at the bottom of the view using table view cells. To do so, you must return an array of SLComposeSheetConfigurationItem objects if you have configurations for the user to set. Otherwise, return an empty array.

Add the following imports to the ShareViewController class.

Add the following variable to the class. This will hold the image selected by the user.

Next we’ll override viewDidLoad() to extract the image from the attachment items in extensionContext. When the user posts an image, it is bundled with other metadata and passed to the extension via the extensionContext object. Add the following to the class.

Here we extract the attachment item in the NSItemProvider object, then check to see if the attachment conforms to the type kUTTypeImage. kUTTypeImage is a system-defined string constant for core uniform type identifiers defined in the MobileCoreServices framework. We use it to identify attachment types.

The image is wrapped in the NSItemProvider class so we first need to load it using loadItemForTypeIdentifier(). If this is successful, we load the image from the url of where it is stored (when extracted from NSItemProvider, images are saved to the disk). We then set the selectedImage variable with the image. Incase of an error, the user will be shown an alert.

Next modify the isContentValid() function as shown.

This check to make sure that an image is selected and the user types in some input before they can post.

Modify didSelectPost() as follows.

Replace the App Group identifier in the statement below with your own.

Here we define the background session we’ll use for the upload. We then define the completion block that will be called when the request completes. In the completion block, we check to see if the upload was successful and if so, save the image (images saved here will be loaded on the container app’s table view). We then delete the temporary image that had been saved to disk during the background request. If the upload failed, we also delete the temporary image and don’t save it for later viewing on the container app. We only want the container app to display images that were successfully uploaded to Imgur.

Note the use of contentText. This property holds the text input from the user.

We then call the uploadImage() function in the ImgurKit framework to do the upload. This method is asynchronous and will immediately return. When the upload completes or fails, the completion block will be called.

Next modify configurationItems() as shown. We return an empty array as we will not be adding anything to the extension’s compose view.

Testing the Share Extension

You can now run the app. Make sure the ImgurUpload scheme is selected. When prompted to choose an app to run, choose Photos. Once running, select a photo and tap on the Share button. On the first run, you will need to add your extension to the share sheet. Select the More button to the right of the other share icons and turn on the switch for your extension then select Done.

image18

Back on the share sheet, you will be able to see your extension icon next to the other share icons. Select your extension, enter some text and tap on Post.

image19

The image will be uploaded to Imgur and saved to the container app. To confirm this, navigate to the home screen and open ImgurShare. You will see the title you entered for the image and the image itself on the detail view.

image20

If you leave the container app and navigate back to the Photos app to share another image, and go back to the container app, you will notice that the table doesn’t automatically update with added item. If you select a table row and then go back to the table view, the table will be updated. We need to reload the table data when the app comes back into the foreground. To do this first add the following function to ImagesTableViewController class.

Then in AppDelegate.swift, add the following to the applicationWillEnterForeground() function.

Now whenever you upload an image via the extension and bring the container app back to the foreground, the table will be updated.

Summary

That concludes this Share Extension tutorial. I hope the guide comes in handy to anyone wanting to create a Share extension. You can download the project files here.

Note: When sharing a file via the extension, you will notice an error message in the debugger “Failed to inherit CoreMedia permissions from xxxx”. This seems to be a common error, and I have encountered it when working with Today, Share and action extensions. The extension still works fine despite the error. Searching around, it seems to be common and in this Stackoverflow post, someone says that their extension works fine and was approved by Apple despite getting the error. So, it might be an Xcode bug. I’m not sure. You can leave your thoughts in the comment.

iOS
Introduction to Create ML: How to Train Your Own Machine Learning Model in Xcode 10
iOS
How To Implement Search Bar in iOS 7 Using Storyboard
iOS
iOS Localization Tutorial: Localize Your Apps to Support Multiple Languages
  • Ravi Kiran

    Ravi KiranRavi Kiran

    Author Reply

    thanks for the gr8 tutorial.I followed the tutorial and created an sample app. It works fine for the first time.
    But when i ran Extension app second time its not showing in Share menu … even thought its is ON, again I need to tap MORE and on and off my app then its displaying. Can I make it visible always in share menu like whats app and Facebook.

    Am I missing any permission in info.plist on in my app?


    • cyn

      cyncyn

      Author Reply

      I have the same problem, but I can’t find a solution to this. 🙁


  • Scotty Fister

    Thanks for the tutorial, it had some helpful info. Though I couldn’t find everything I needed in one place so I did a write up once I did: https://medium.com/@scottyfister/how-to-build-an-ios-share-extension-in-swift-4a2019935b2e#.9q4f48w4f


  • Kahna Nau

    Kahna NauKahna Nau

    Author Reply

    After adding share extension getting error of None of the valid provisioning profiles allowed the specified entitlements : com.apple.developer.icloud-container-identifiers, com.apple.security.application-groups.


  • Payal. Maniyar

    It shows error of swift in Xcode . Is there latest sample code with swift 2.2?


  • Kahna Nau

    Kahna NauKahna Nau

    Author Reply

    how to use share extension for other types of files else then images?


  • Amir Jani

    Amir JaniAmir Jani

    Author Reply

    i want write a music player with share extension but i cant save data and my data that is content of url is always return null , what should i do?


Leave a Reply to Scotty Fister
Cancel Reply

Shares