In previous chapters, you saw how to implement documents on iOS and macOS to save data and share it across your apps. In this chapter, you’ll see another way to share data using the Share button so that you can dynamically share data from one app to another without using a document to store and share the data.
The main points covered in this chapter are
Using Share buttons (as a user)
Using Share buttons (as a developer)
Managing the shared data
Using Share Buttons (As a User)
To share data, you need one app to share the data (sometimes this process is called vending the data) and another app (or several apps) to receive the data and use it as the receiving app wants. Note that, just as is the case with documents and their types, the connection between sender and receiver (or vendor and receiver) is dynamic. Neither app needs to know about the other. The document type or sharing information lets each app function on its own so that you don’t need to build enormous multi-purpose apps that require substantial development and maintenance costs.
Sharing is part of Cocoa and Cocoa Touch (and it has been since the beginning of the original NeXTSTEP and Rhapsody versions of the operating systems). In order to share data, a common format needs to be available to the sender and receiver. Here is an example of the basic structure.
Creating a Sharing Example
Note
Remember that you can add apps to the simulator. Mail may not be configured for you. If you have a device you can use for testing, it’s best to use it. You can test the ShareApp by downloading it as described in Chapter 1. Look for the Chapter 8 version.
The simplest example to use for sharing data is the Master-Detail project template in Xcode. It’s the basis for many examples (and even apps in the App Store). It’s a simple app with two views. On an iPhone, only one view at a time is shown; on larger devices, the two views share the screen. Whichever you’re using, you start from an app that lets you tap + to enter the current date and time, as you can see in Figure 8-1.
If you tap the timestamp created in the master view, you’ll see the details (a better-formatted timestamp) shown in Figure 8-2.
In Figure 8-3, you can see an action button added to the navigation bar at the right. (You’ll learn how to implement it in this chapter). With the action button, you can share the data from the detail view.
The example used here is named ShareApp. Start from the Master-Detail App template shown in Figure 8-4.
Create your own version of the app as shown in Figure 8-5.
As you can see in Figure 8-6, you don’t need to worry about any settings at this point because the defaults will work for you.
Open the storyboard. You’ll see the two main views shown in Figure 8-7.
Open the library at the top of the utilities pane to see the library objects, as shown in Figure 8-8.
Drag a bar button item to the right of the top bar in the Detail scene, as shown in Figure 8-9.
From the attributes inspector, choose the Action button, as shown in Figure 8-10.
Implement the action button by opening the storyboard with the assistant so that you can see the storyboard and DetailViewController at the same time, as shown in Figure 8-11. Control-drag from the action button in the storyboard to the code in DetailViewController and name the outlet actionButton.
Add the code for the action, as shown in Figure 8-12.
Set a breakpoint in the actionButtonAction function and test that your action button now works.
All that’s left to do is to implement the sharing.
Sharing the Data
The objective at this point is to share the timestamp data in a format that other apps can recognize. This is done by implementing a UIActivityViewController, as you will see in this section. The activity view controller that you present will reflect the type of data that you have available to share and the possible receivers of that data. Because of this dynamic functioning, you can’t be certain what you should be seeing, so the first example in this section will use a very basic type of sharing: the activity view controller will share text that is included in this section. For most users, this text will be able to be received by built-in apps such as Mail, Notes, and Messenger.
Your activity view controller will present a list of items available to share. For this example, you can do that with the code shown in Listing 8-1. Add this code to DetailViewController.
In this example, you need a receiver that can handle plain text. Mail, Notes, and Messages can do so, but if you do not have them installed, your choices will be limited to Save or Copy. Otherwise, you will see an activity view controller, as shown in Figure 8-14.
If you have installed Mail, you can send the text, as shown in Figure 8-15.
Summary
Experiment with various combinations of senders and receivers. The set of items passed to the activity view controller is a set of type Any. For reference, here is the declaration of init for UIActivityViewController: