Share Extention in iOS App

Share extensions was introduced in iOS 8. Share extension is useful for sharing content like images, videos, text, etc. in your app from any app. 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 share action. But by using share extension in iOS app, there is no need to open an app to share any content.

Implement Share Extension with Steps

CallKit is a new framework and is available in iOS 10 and later. Used to implement VoIP functionality, call blocking, and identification in your app. Apps can use CallKit to receive incoming calls and outgoing calls using the native call UI.

First you need to create an app, in which you share content from other app.
share-extention1

Now create share extension, go to File->New->Target.
share-extention2

Select Share Extension from Application Extension. Provide a name of your Extension.
share-extention3

share-extention4

Now XCode will ask you to active the scheme, Click on Active button.
share-extention5

It will include following list of file in your project folder.
share-extention6

Enable App Groups.

  1. Select project target and go to Capabilities.
  2. Select development team and turn on App Group
  3. Now list of app groups will be shown. Select your app group.
  4. Now select extension from target and do the same to enable app group in share extension.
  5. Now app is configured and ready to share content.

share-extention8

share-extention7

share-extention9

Now build and run your extension by choosing extension from active scheme as following.
It will ask to select an app to run, here we will select Photos and press Run.

  1. It will directly open photos.
  2. Now select any photo from photos and click on share button.
  3. If you don’t get extension name, then click on more and enable extension from list.

share-extention12

share-extention10

share-extention11

share-extention13

Now click on that and get following screen.
share-extention15

Composer sheet is available with content and image. Click on the Post button. But nothing happens. Now if you want to handle delegate method, when user click on post button, respected content will be shared to your application you have to go through the extension classes.

Extension Classes

  1. When Share extension is added by default ShareViewController.swift, and MainInterface.storyboard is created.
  2. By default ShareViewController.swift inherits SLComposeServiceViewController, that have it’s UI for share extension.
  3. If you want your own UI design, then you can inherit UIViewController instead of SLComposeServiceViewController in ShareViewController.swift file.

Following steps describe line of code that you have to write in your share extension class files ShareViewController.swift.
isContentValid: This method is used to validate the content like sharing text length, etc.

didSelectPost: This method is called when user press the post button. This is the place where you can do sharing. The method completeRequestReturningItems([], completionHandler:) must be called for uploading task, so host app can do stuff without blocking main thread.

Make change in info.plist in your share extension for share image.

Setting this property extension is shown when only one image is selected. We can set this property to hide or show extension when more than one image is selected.

Show shared content in your app

Add following code in your ViewController to get shared image and text.

Check image sharing

Run your project (Select your app as a target not the extension).
First time image and text gets empty.
share-extention16

Now stop app and run your extension, select photos app. Photos app now opened. Select any photo, tap on share button. Select your app extension form activityViewController, if your app is now listed then go to More and switch on your extension to share.
Now tap on your extension, add some text and press on Post button.
share-extention17

Close Photos app and open your app, you will see selected photo and entered text in your app now.
share-extention18

I hope you find this blog very helpful while working on Share Extension.
Thank you.

mm

Pravin Kambariya iOS Developer

I am an iOS Developer at Yudiz Solutions Pvt. Ltd. – an iPhone App development company. I always come up with innovative ideas which can be intended as a part of organizational development.

Comments are closed.

Top