Adding Custom Activity in UIActivityViewController

Sonal Bellani
HealthifyMe Tech
Published in
Nov 18, 2020

Almost every iOS app developer knows about UIActivityViewController, in our iOS apps we often need to share content to external apps for which we use UIActivityViewController. But do you know, you can easily share content within your app through UIActivityViewController just by adding a custom activity to it. You can also add custom activity to have other functionalities as well apart from sharing such as archive content, reverse string, etc.

Basic code of UIActivityViewController:

let text = “Share Achievement”
let image = UIImage(named:”shareImg”)
let activityC = UIActivityViewController(activityItems: [image, text], applicationActivities:nil)

The initializer method of UIActivityViewController has two arguments named activityItems: and applicationActivities: . In the first parameter we must pass the array of items to share. Have you ever thought, what to pass inside applicationActivities in the above code?

So, this applicationActivities: is the place where we pass our custom activity. First let’s talk about how to create custom activity.

To create an application activity we subclass the UIActivity and override the methods and properties of UIActivity class.

To use this in UIActivityViewController pass the instance of CustomActivity to the applicationActivities parameter of the initializer method of UIActivityViewController.

let activityC = UIActivityViewController(activityItems: [image, text], applicationActivities:CustomActivity())

Congratulations, you have created your first custom activity. That was so easy, isn’t it?

In HealthifyMe iOS app, we have used custom activity to add “HealthifyMe Groups” option which let user to share their achievements within the app in different groups.

--

--