iOS Share Extension

Abhishek Thapliyal
5 min readSep 20, 2017

--

In iOS 8 release, Share extension is introduced, which allows you to share content to social services like Facebook, twitter and also other utility apps.
In this blog you will learn 2 things.

1. Share Data between host app and extension
2. Open host app from extension

Let’s start basic image picker app, that pics images from gallery and show in view. Create a sample project Image Picker.

Now In the Main.storyboard, Embed the view controller in the navigation controller.

Drag bar button from right object library to navigation bar of view controller. Set the button system item “Add”. Set the selector of button to view controller named “openPhotos”.

Now add collection view to the view controller as shown in the above screenshot. Select collection view and select size inspector in right side. Set cell spacing and section insets as shown

Drag its outlet to ViewController class named “imgCollectionView”.
Add a new file for custom class of collection view cell named “ImageCollectionCell”. Go to storyboard agin set reusable Identifier as “ImageCollectionCell” and set custom class “ImageCollectionCell”. Drag ImageView from object library to collection cell and set outlet named “imgView” to the custom class “ImageCollectionCell”.

define CellModel struct in “ImageCollectionCell” and function configure as shown below

Come to view controller class and setup imgCollectionView’s (datasource and delegate) and imagePicker’s (UIImagePickerControllerDelegate and UINavigationControllerDelegate) shown below

Now after all above is done run the app and you will find that app crashes. For accessing images from gallery we need permission. So open info.plist, add a new row and and write(it will predict) Privacy — Photo Library Usage Description and set ImagePicker wants to use Photos value.

Re-run app and tap on + button in the navigation bar.

Open any album and pick image. After picking you will se imagePicker is dismissed and a image is added in collection view.

You add as may as images. Let’s move forward to add Share extension.

For adding share extension Go to File > New > Target

Add name ImageShare to the extension

Now in the MainInterface.storyboard add a collection view and add image view in cell.

Set cell custom class ShareImageCollectionCell and set image view outlet.
Add left and right navigation bar button namely : Cancel and +. Now the setup delegate and data source. ShareViewController file look like this

I have added cancel and next button action to cancelAction and nextAction respectively. Now manageImage() will manipulate images that are selected from Photos.

Extension of UIImage is used for resize image as when 20–30 pics are selected and saved will get memory issue. To avoid that we have to resized/cropped image size.

NSExtensionItem provides images as an attachment. So here i’m appending images into array in the form of Data to save in NSUserDefaults(swift: UserDefaults).

You will see the suit name is different. Yes, for data sharing between extension and host-app we need to setup App-Groups in capabilities section. Open ImagePicker target > Capabilities. Enable App groups and select group.YOUR_BUNDLE_ID.

and do same in extension target ImageShare.

Also for open app from extension we need to configure URL schemes.
Go to ImagePicker i.e host app target. Open info section and in the bottom add url scheme

Now you will see a method redirectToHostApp() in ShareViewController Class. This method redirect from extension to host app. In that dynamically iterating responder chain to get UIApplication Object of app once it get it will call the URL selector.

self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)

this will complete extension process so extension will be closed once your app is opened.

In you app delegate you have to add openURL delegate.

With the same UserDefaults group name fetch array of images(in the Data form) and map in CellModel. For safe side we have appended key with URL parameter.

Let’s run the app and then extension. Go to schemes and select ImageShare. Tap on run button that will ask host app select ImagePicker. App will open go to background and open Photos. Select images and tap on share button

You will see the ImageShare option is available this is our extension name. Tap on it.

This will open ShareViewController. On clicking Cancel this will dismiss and move back to Photos. On clicking Next it will open host app.

And that is our host app.

Hola!!! you have successfully transferred images from Photos to your app without opening app directly.

Misc: If you want to add limit how much photos to be selected from Photos then update extension’s info.plist

NSExtensionActivationSupportsImageWithMaxCount set required value.

You can download project from here.

--

--