Share Extension in iOS Application – Overview with Example

Extension, as the name suggests, it is a part of an iOS application which extends the iOS application’s functionality and makes the app available in other parts of the system like Notification Center and other apps. Apple’s iOS provides different kinds of extensions for different purposes and those can be used at different system areas such as Notification Centre, Keyboard, Photo editing, etc.

In this tutorial we will see about one such extension provided by iOS with example, i.e. Share Extension.

Share Extension

Share extension is useful for sharing content like images, videos, text, etc. and sharing urls. Previously (before iOS8), to share the content, it was done by first copying the content and then open an app in which we want to share and then perform action. But by implementing share extension in iOS app development, there is no need to open an app to share any content.

Creating Share Extension with Example

Extension isn’t a standalone app, so first it is necessary to create an iOS app.

After an app is created, go to File->New->Target

Then select Share Extension from Application Extension. Provide a name of your extension.

Then Xcode will ask you to activate scheme, so click Activate it.

Enable App Groups.

Select project target and go to Capabilities.

Turn on App group and then select the Development team.

After that the list of app groups will be shown, select app group.

Now select extension and do the same to enable app groups.

Now app is configured for sharing content.

Extension Classes

When Share extension is added by default ShareViewController.h, ShareViewController.m and MainInterface.storyboard is created.

By default ShareViewController.h inherits SLComposeServiceViewController, that have it’s UI for share extension.

If you want your own UI design with full customization of share extension, then you can inherit UIViewController instead of SLComposeServiceViewController in ShareViewController.h file.

There are three methods specified in the ShareViewController.m that help in sharing the content.

isContentValid : This method is used for validating the content like sharing text length shouldn’t be zero, etc.

didSelectPost : This method is called when user press the post button. This is the place where you can do sharing of images, uploading videos, etc. The method completeRequestReturningItems([], completionHandler:) must be called for uploading task, so host app can do stuff in background thread and does not block main thread.

configurationItems : By returning an array of SLComposeSheetConfigurationItem, you can customize the default share extension’s view. The items will be showed below the share extension view.

What Changes we need to make ?

Select extension target and Go to Capabilities->App Group and turn it On.

Select Development team from the dialog box that appears after turning on app group.

Then Select app group. Select the project target and Go to Capabilities and do the same.

This is how the app group name is used for sharing in application.

When user press Post button then this method will be called. In this method we are storing image and it’s name in user defaults.

To get image,

To send image,

Make changes in Info.plist setting for the image share extension.

Now run the application and you can see the how the image is being shared.

How to check image sharing ?

Run your project (Select your project target and not the extension), what you will see is the image and the label but they will be empty.

Now stop the app and run your extension and Choose Photos app. Photos app is now opened. Now select more from ActivityViewController where you will see your extension and turn it on.

After selecting image click on your share extension.

Now you will see the view that is actually a SLComposeServiceViewController. Enter text and press post button.

Now close Photos App and open your app. You will see the selected image and entered text in your app.

Here’s a short video for better understanding.

This is how Share extension works and makes it very simple to share the images to our iOS apps like any other sharing application. I hope this share extension tutorial was helpful enough to understand the working of share extension. So this was all about Share extension with example in iOS application development. Have any questions, drop it in comment box.

iOS Team @ Weblineindia

We are iOS Team @ Weblineindia.

We are energetic, adaptive and enthusiastic to explore our limits in this dynamic world of Mobile App Development. We always remains up-to-date with industry standards and trends that are followed for mobile application development. We have so far delivered applications in various domains like Business, Health & Fitness, Social Networking, Utilities and many more. We have also delivered many enterprise applications and complex architecture applications which follows SAAS model.

We have expertise in developing iPhone / iPad Applications, Universal Apps, iWatch Applications and Mac OS Applications which includes many rich features like offline data storage using core data / sqlite framework, cloud based APIs, usage of widgets and extensions, push notifications, in-app purchase functionality, siri enabled features and many more.

As a team, we are always eager to contribute back to the community and so happy to help here to our developer community on any technical problems and discussions.