In our earlier post, we explained how to build a Share Extension using Swift 3.0. In this guide, we will walk through the same process using Swift 4.0.
The main objective of building a Share Extension is to enable an app to save a URL as rich content and configure custom options directly from the iOS share sheet. A Share Extension allows your app icon to appear in the system share menu and handle content shared by the user without switching manually between apps.
Building a Share Extension in iOS using Swift is straightforward and helps simplify content sharing workflows in mobile app development.
Laying the Groundwork
You can either:
- Create a new Xcode project
- Open an existing project where you want to add the Share Extension
Step 1: Add a Share Extension Target
- Go to File > New > Target
- Select the iOS tab
- Choose Share Extension
- Click Next and rename it as required
To test your extension properly, activate the option to run the app using a browser such as Safari. This ensures smoother testing and responsiveness.
Once set up:
- Run the project
- Open Safari
- Tap the Share icon
- Look for your extension name
If it does not appear, tap the More button and enable it.
When you select your extension, it will load the default implementation associated with SLComposeServiceViewController.
Fetching the URL
Once enabled, the share sheet automatically retrieves certain information such as:
- Page title
- Basic metadata
However, for advanced use cases, you may need additional details from the webpage. To achieve this, you can run a JavaScript file to parse the document and extract more information.
Step 2: Create a JavaScript File
- Go to your Extensions folder in Xcode
- Select New File
- Under the Other tab, choose Empty
- Rename it to GetURL.js
Step 3: Update Info.plist
Edit the extension’s Info.plist file and:
- Add NSExtensionJavascriptProcessingFile under NSExtensionAttributes
- Set its value to GetURL without the .js extension
- Add NSExtensionActivationSupportsWebURLWithMaxCount under NSExtensionActivationRule
The system looks for an object named ExtensionProcessingJS at runtime inside the JavaScript file.
For detailed implementation references, consult the official documentation from Apple.
Step 4: Retrieve Data in Swift
In ShareViewController.swift:
- Import kUTTypePropertyList
- Retrieve the processed data inside viewDidLoad
When you run the extension, the URL will now print in the console.
Customizing and Adding Configuration Cells
You can enhance the Share Extension by adding configuration items at the bottom of the share sheet.
Configuration items allow users to:
- Select categories or decks
- Save preferences
- Trigger additional JavaScript processing
When the Share Extension target is created, it includes boilerplate code that supports SLComposeSheetConfigurationItem.
Step 5: Create a Model
Create a simple model file called:
Deck.swift
This model will help pass selected data between view controllers.
Creating a Table View for Selection
To display selectable options:
- Create a new UIViewController file
- Name it ShareSelectViewController.swift
- Add a table view property
- Populate it with dummy data
- Add it inside viewDidLoad
Next, instantiate ShareSelectViewController inside the tapHandler of your configuration item.
Run the app after each update to ensure proper mobile app testing. When tapping the configuration cell, the table view should appear with dummy data.
Updating the Selected Deck
Initially, the configuration cell may display a default label such as “Deck Title”.
To update it dynamically:
- Create a property to store the selected deck
- Set a default value
- Update it inside configurationItems()
To handle navigation:
- Create a protocol for delegation
- Conform ShareViewController to that protocol
- Set the delegate to self in ShareSelectViewController
After implementing delegation and running the app again, the selected deck updates automatically in the configuration section.
Sending Information to the Server
Once all data is collected, the final step is sending it to the server or sharing it with the main app.
You can use:
- NSURLSession to post data to your backend server
- App Groups to share data between the extension and the main app
- NSUserDefaults to store and retrieve shared data
Replace dummy data with actual configured data before production deployment.
Additional Customizations
- Change the extension icon through project settings
- Modify the Display Name in extension settings to update its name in the share sheet
Conclusion
Building a Share Extension in iOS using Swift 4.0 allows developers to enhance content sharing and improve user experience. By integrating JavaScript processing, configuration cells, table views, and server communication, you can create a powerful and user friendly sharing workflow.
A well implemented Share Extension:
- Simplifies cross app interaction
- Enhances content handling
- Improves productivity
- Strengthens user engagement
If you are planning to enhance your iOS app with custom Share Extensions, following this structured approach will ensure smooth integration and scalability.
For implementation examples and code references, you can explore official developer documentation and community supported repositories for further guidance.
