Multiple Selections and Sharing Collection View Items Using UIActivityController
Today we are going to implement UIActivityController in order to share images of the Cities app which we have already created in the previous tutorial. If you haven’t check it out yet, you can find it in the link below:
Making a Simple Photo Gallery Using UICollectionView (Swift 5)
Also, you need to download the following related app in order to continue this tutorial:
Let’s start if you have finished the downloading!
Remember, when you are sharing pictures with your friends, you are selecting multiple pictures and sending them all at once instead of sending them one by one most of the time. By default, the Cities app allows users to select a single item. So, the first thing that we are going to implement is multiple selection.
Let’s start with adding the Share button to the navigation bar of CityCollectionViewController. Then go to Main.storyboard, drag Bar Button Item from Object Library and put it in the navigation bar. Set the title to Share.
Insert an outlet variable and an action method for the Share button in CityCollectionViewController:
Connect the Share button with the shareBtnTapped method. Also, associate it with the shareBtn outlet.
To support the multiple selection mode, we will add two variables to CityCollectionViewController.
How can we take a snapshot of the cell? Since a collection view cell is a subclass of an UIView class, we will create an extension in order to empower the snapshot capability of the cell. In the project navigator, right click and create a new group called Extension. Then right click the Extension folder and create a swift file. Name it Snapshot. Add the code below into this new Snapshot.swift file.
This is how we take the snapshot of a view. Let’s continue to develop the Share feature. Update the following method in CityCollectionViewController like it shown below.
When we are making a multiple selection, it is better to understand exactly which images have been selected. To do that, we need to highlight the selected images. Edit the cellForRowItemAt method like it is shown below;
skyBlue is an image for the background. Download it in the link below, and add to the Assets in the project navigator.
Just as we need the selection method, we may as well need a method for deselection. When an item is deselected, it should be removed from the selectedImages array. Add the code below into the CityCollectionViewController:
We haven’t done anything about the shareBtnTapped button yet. We will implement the method of the Share button. It will work once the user taps the Share button itself. Update the method with the following code:
The coding part is almost done, but if you run the app, you will face an error. Because, the segue will be triggered every time you tap an image. We do not want to trigger the segue in multiple selection. We only want to trigger the segue when it is in the single selection mode.
Lastly, change the didSelectItemAt function like shown below:
That’s all! Run the app and try to share.
You can download the completed version in the link below.