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.
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.
|NSExtensionActivationSupportsAttachmentsWithMaxCount||Include this key to indicate to the system and to other apps that your app supports a maximum number of attachments.|
|NSExtensionActivationSupportsAttachmentsWithMinCount||Include this key to indicate to the system and to other apps that your app supports a minimum number of attachments.|
|NSExtensionActivationSupportsFileWithMaxCount||Include this key to indicate to the system and to other apps that your app supports files in general.|
|NSExtensionActivationSupportsImageWithMaxCount||Include this key to indicate to the system and to other apps that your app supports image files.|
|NSExtensionActivationSupportsMovieWithMaxCount||Include this key to indicate to the system and to other apps that your app supports movie files.|
|NSExtensionActivationSupportsText||Include this key to indicate to the system and to other apps that your app supports text.|
|NSExtensionActivationSupportsWebURLWithMaxCount||Include this key to indicate to the system and to other apps that your app supports web URLs.|
|NSExtensionActivationSupportsWebPageWithMaxCount||Include 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