Home » UIActivityViewController in iOS

UIActivityViewController in iOS

by Online Tutorials Library

UIActivityViewController in iOS

The UIActivityViewController is an object used to offer standard services like sharing items in the app through the activity items available on the device. The UIActivityViewController inherits UIViewController class.

The UIActivityViewController is declared as below.

There are several standard services like copying items to the pasteboard, sending app items using email, SMS, or any other option available on the device, posting content to the social media sites, etc. We can also define our custom services in the application.

Our app configures, present, and dismiss the UIActivityViewController. However, we must define the data objects before presenting the UIActivityViewController. We can also specify the list of custom services our app supports. On iPad, the app must present the UIActivityViewController in the popover, whereas, on iPhones and iPod touch, we can present it modally.

In this tutorial, we will create an iOS app that uses UIAcitivityViewController to use standard services in our app. We will create a custom share button that will present the options to share the items in the app.

Let’s create the XCode project as ActivityDemo, as shown below.

UIActivityViewController in iOS

Once we are done with creating the project, let’s configure the View Controller in interface builder. Embed the view controller in Navigation Controller, define the title for the view controller in the navigation bar, and add a bar button item triggered for sharing.

UIActivityViewController in iOS

In this app, we will add a table view as well, which will display a vehicle list to share a particular vehicle after selecting them.

Let’s add a table view to the View Controller.

UIActivityViewController in iOS

Let’s create the subclass of UITableViewCell, assign it to our cell, and connect outlets for the image view and title label.

Add the following code in ViewController.swift to configure the table view.

Now, if we run the app on the simulator, the home screen will look as shown below.

UIActivityViewController in iOS

To share any vehicle, the user needs to select that from the list and click the share button added as the right bar button item. For this purpose, let’s connect the action outlet for the right bar button item and didSelectRowAt(: ) delegate method for tableview in the view controller.

Let’s define the following property in ViewController to store the selected vehicle name when the user selects any table view row.

Now, add the following method in the tableview delegate methods.

Let’s code the share button event inside clickedShareButton. We will present the activity view controller once the user clicks on the share button.

Before presenting the view controller, we also need to validate whether the user has selected any row before clicking the share button.

Now, run the app, select any vehicle to share. The app will run as shown below.

UIActivityViewController in iOS

The app will display more options like email, WhatsApp, etc., depending upon the sharing options available on the iOS device.

By default, the activity controller shows all the activities available for the provided items type either image or string. However, we can exclude the activity types for the activity controller, as shown below.

This will not display the copy option in the activity controller.

The ViewController.swift contains the following code.

ViewController.swift


You may also like