9series
iOS

How to Build a Share Extension in iOS Using Swift 4.0

December 12, 2019

Share Extension in iOS Using Swift 4.0

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 

  1. Go to File > New > Target 
  1. Select the iOS tab 
  1. Choose Share Extension 
  1. 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 

  1. Go to your Extensions folder in Xcode 
  1. Select New File 
  1. Under the Other tab, choose Empty 
  1. 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: 

  1. Create a new UIViewController file 
  1. Name it ShareSelectViewController.swift 
  1. Add a table view property 
  1. Populate it with dummy data 
  1. 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: 

  1. Create a property to store the selected deck 
  1. Set a default value 
  1. 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. 

Read our Recent Articles

AI Powered Community Banking: Why Automation Is the New Relationship Moat in Modern Banking
AI Solutions

AI Powered Community Banking: Why Automation Is the New Relationship Moat in Modern Banking

What is AI Powered Community Banking?  AI powered community banking is the use of artificial intelligence, built on a...

10 min read • Mar 25, 2026 Read more
Deep Learning and Machine Learning: Understanding Their Synergistic Relationship in Modern AI
AI Solutions / Artificial Intelligence

Deep Learning and Machine Learning: Understanding Their Synergistic Relationship in Modern AI

Artificial Intelligence has evolved rapidly over the past decade, largely driven by breakthroughs in Machine Learning and Deep Learning. While...

3 min read • Apr 29, 2024 Read more
AI and IoT Integration: Powering the Next Generation of Smart Connected Environments
AI Solutions / Machine Learning

AI and IoT Integration: Powering the Next Generation of Smart Connected Environments

Introduction  The convergence of Artificial Intelligence and the Internet of Things marks a major milestone in digital transformation. As businesses...

4 min read • Apr 19, 2024 Read more

Let's Discuss Your Strategic Technology Initiatives

Partner with 9series to accelerate your digital transformation journey. Our enterprise architects are ready to design solutions tailored to your unique challenges.

Trusted by global partners

Nailbiter NUs Safaricom Intuify Solvit i-banq Fractal