App extensions are used to extend the functionality of iOS apps to other parts of the mobile system and to allow for better inter-app communication from in build app to third party app.
We can create an app extension to share the content with the third party app or with in build apps. There are many examples for such type of extension, let us consider users want post to our social service from a web browser, we can provide a Share extension. Or, users want to catch up on their favorite team. We can provide a Today widget that displays current sports scores in Notification Center.
If user have capture photo using mobile camera, he can directly post capture image to social media. But if he wants to do some edit in photo, at first user need to edit picture in some app and then post it to social media. This type of functionality can be achieved from Image sharing extension.
We are going to build a simple Image share extension called “Image Share”. Image can be share directly from gallery to the container app.
We need to create project-using Xcode (Integrated development environment) by launching Xcode. Select New > Project from Xcode’s File menu and choose Single View Application from the list of templates.
After that give project a name of extension with organisation identifier. We are going to use objective-c as development language.
After project creation, we are going to add a target of extension. We need to select New > Target from the File menu. From left panel, select Application Extension from iOS section, choose Share extension.
Set the Product Name. Also note the other options, specifically the Action Type. Click Finish to create the Action extension.
Share extension Types
There are two type of Share extension, First type have user interface and second with no user interface. First type will open popup view in the gallery with the thumbnail of selected image. Popup view will have header with right side button to post it and left side button to cancel the share action.
In extension we are sharing image from in build app to our container app. For that we have to specify extension of sharing content in the containing app. All the information regarding extension of share contains is need to be feed in the container app info.plist. In our extension define image max count and web url max count same as given below with extension attributes.
In extension we need to pass image from gallery to our container app. In iOS we cannot easy pass contain from one app to other app because of access permission issue. App group allows multiple apps to share/access contain from one app to other. They can also share same directory to save, add and update files.
For that we need to define group in app entitlement, in extension also we need to add access permission for add group. So, that share contain can be used by container app and extension. To set the group setting for app and extension, in target go to capabilities tab and turn on app groups also add group name.
Step 5: Implement ShareViewController
As we have create the extension default shareviewcontroller.h/.m is create for image share extension in container app. We need to do following changes. Below code will pass selected item to container app.
Below code will save pass selected item to group folder app.
At the end – ( void ) invokeApp: ( NSString * ) invokeArgs will call the container app with passed selected image path.