ClickCease
Abstract visualisation of UIActivityViewController

A Guide to Post Sharing with UIActivityViewController

Mark Worachote
Mark Worachote
Solutions Engineer
iOS
Jan 14, 2024

In the digital age, the ability to share content across various platforms is a crucial feature for any application. Whether it’s a photo, a blog post, or a news article, users want the ability to share what they find interesting with their friends and followers on social media. In this blog post, we will delve into the intricacies of sharing posts to social media platforms like Facebook and Instagram using Swift’s UIActivityViewController.

This guide will provide clear, step-by-step instructions to ensure you can seamlessly integrate this feature into your own applications. Whether you’re a seasoned developer or a beginner, this guide will be a valuable resource in your toolkit. We will cover everything from setting up the UIActivityViewController to testing the sharing feature.

Pre-requisites

Before we dive into the tutorial, there are a few things you’ll need. First, you’ll need a basic understanding of Swift and iOS development. This is crucial as we will be using Swift’s UIActivityViewController for sharing content. Second, you’ll need Xcode installed on your Mac. Xcode is the development environment we will be using to write and test our code. Third, you’ll need an iOS device or simulator for testing. This is where you’ll see the fruits of your labor come to life. Lastly, you’ll need a Facebook and/or Instagram account for testing the sharing feature. These platforms are where we will be sharing our content.

Step 1: Setting up the UIActivityViewController

The first step in sharing content to social media is setting up the UIActivityViewController. This is a built-in view controller in iOS that provides a standard interface for sharing content. You can initialize it with the content you want to share, and it will handle the rest. Here’s a simple example of how to set it up:

In this code snippet, we’re initializing the UIActivityViewController with a string of content and presenting it to the user.

Step 2: Customizing the UIActivityViewController

By default, the UIActivityViewController will include all possible sharing options. However, you can customize this to only include the options you want. For example, if you only want to allow sharing to Facebook and Instagram, you can do so by setting the excludedActivityTypes property. This property accepts an array of activity types that you want to exclude from the sharing options. Here's how you can customize it:

In this example, we’re excluding all sharing options except for Facebook and Instagram.

Step 3: Passing the Shared Data

After setting up the UIActivityViewController and customizing the sharing options, the next step is to pass the shared data into the sheet and then into the other app. This is done by using the activityItems parameter when initializing the UIActivityViewController. The activityItems parameter is an array of the items you want to share. These items can be any type of object, as long as they conform to the UIActivityItemSource protocol. Here's an example:

In this example, we’re sharing a string and a URL. When the user chooses to share this content, the UIActivityViewController will pass these items to the chosen app.

Step 4: Handling the Completion Handler

The UIActivityViewController provides a completion handler that you can use to perform actions after the user has shared the content. This could be anything from showing a confirmation message, to tracking analytics data. The completion handler is a closure that gets called when the user completes the sharing action. It provides four parameters: the activity type, a boolean indicating whether the sharing was successful, an array of the shared items, and an error if one occurred. Here’s how you can use it:

In this example, we’re simply printing a message based on whether the sharing was successful or not.

Step 5: Testing the Sharing Feature

Now that you’ve set up the UIActivityViewController, it’s time to test the sharing feature. Run your app on an iOS device or simulator, and try sharing some content. You should see the sharing options you specified, and be able to successfully share the content to Facebook or Instagram. This is the final step in our guide, and it’s where you’ll see if all your hard work has paid off. Remember to test on different devices and with different content to ensure a smooth user experience.

Final Thoughts

Sharing content to social media is a powerful feature that can greatly increase the reach of your app’s content. With Swift’s UIActivityViewController, it’s easier than ever to add this feature to your app. Whether you’re a seasoned developer or a beginner, we hope this guide has been a valuable resource in your toolkit.

If you’re looking to enable chat, social or live-streaming experiences in your platform, Feel free to explore the features Amity Social Cloud offers, If you find that a pre-built solution aligns with your business goals, you can start right now by contacting Amity!