Mallow's Blog

Understanding Share Extension

Share Extension

    How is Share Extension a ‘hack’? How do I integrate content into my app? You are at the right place already if you have questions on any of these topics.

Apple offers the Share Extension as a simple means of transferring content (such as files, photos, audio, screenshot, link and location) between apps, even though they are created by different developers. In this, we will learn how to build share extensions and how to use them.

How to create a Share Extension?

You will need a Share Extension when other apps want to share data with your app. Share sheet will appear when we try to share any of the content from the other apps as like below.

Alright! To begin we will do some basic setup to show our app in the share sheet like above image.

Add a Share Extension to your project

    In order to show our app we need to create share extension target. File -> New -> Target -> Share Extension. 

Activate the share scheme

    After creating the share extension target, the system asks you to activate the share scheme, simply choose Activate. Because XCode creates a new Target with an associated scheme for the extension, this is necessary. The new target is seen in the target list.

The new controller and share extension info.plist will be created. After creation the share extension’s minimum deployment target will be the xcode latest version. So, we need to change the deployment target to which our app supports otherwise our app will not be visible in the share sheet while we try to share any content.

Add the group name to target share extension and host app

We must set up App-Groups in the section on signing and capabilities in order to share data between the extension and the host app. Activate app groups and choose a group.

We need to add the containerURL(forSecurityApplicationGroupIdentifier:) to Return the container directory associated with the specified security application group identifier.

We need to add the activation rule in order to share the data content from other apps to our app.

NSExtensionActivationRule

The below table lists the keys that you can include in the dictionary associated with the NSExtensionActivationRule dictionary. The value for each key is an integer that specifies the maximum number of files of the corresponding data type that your extension can handle.

KeyDescription
NSExtensionActivationSupportsAttachmentsWithMaxCountInclude this key to indicate to the system and to other apps that your app supports a maximum number of attachments.
NSExtensionActivationSupportsAttachmentsWithMinCountInclude this key to indicate to the system and to other apps that your app supports a minimum number of attachments.
NSExtensionActivationSupportsFileWithMaxCountInclude this key to indicate to the system and to other apps that your app supports files in general.
NSExtensionActivationSupportsImageWithMaxCountInclude this key to indicate to the system and to other apps that your app supports image files.
NSExtensionActivationSupportsMovieWithMaxCountInclude this key to indicate to the system and to other apps that your app supports movie files.
NSExtensionActivationSupportsTextInclude this key to indicate to the system and to other apps that your app supports text.
NSExtensionActivationSupportsWebURLWithMaxCountInclude this key to indicate to the system and to other apps that your app supports web URLs.
NSExtensionActivationSupportsWebPageWithMaxCountInclude this key to indicate to the system and to other apps that your app supports web pages.

After adding the extension action rule, our app is ready to receive the content from the other apps. You can now attempt to launch the application (be sure to launch your primary target and not the shared one) and attempt to share stuffs such as images, map links, etc. After sharing content, your app should appear in the share view list and you should get it in the share extension controller. You can now customise the contents in the app.

Mukesh, iOS Team

Mallow Technologies Pvt Ltd

Leave a Comment

Your email address will not be published. Required fields are marked *