© Jesse Feiler 2019
Jesse FeilerImplementing iOS and macOS Documents with the Files Apphttps://doi.org/10.1007/978-1-4842-4492-0_8

8. Sharing Documents with Share Buttons

Jesse Feiler1 
(1)
Plattsburgh, NY, USA
 

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.
../images/465467_1_En_8_Chapter/465467_1_En_8_Fig1_HTML.jpg
Figure 8-1.

Creating a new timestamp record

If you tap the timestamp created in the master view, you’ll see the details (a better-formatted timestamp) shown in Figure 8-2.
../images/465467_1_En_8_Chapter/465467_1_En_8_Fig2_HTML.jpg
Figure 8-2.

Detail view

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.
../images/465467_1_En_8_Chapter/465467_1_En_8_Fig3_HTML.jpg
Figure 8-3.

Adding an action button to share the data

The example used here is named ShareApp. Start from the Master-Detail App template shown in Figure 8-4.
../images/465467_1_En_8_Chapter/465467_1_En_8_Fig4_HTML.jpg
Figure 8-4.

Starting from the Master-Detail App project template

Create your own version of the app as shown in Figure 8-5.
../images/465467_1_En_8_Chapter/465467_1_En_8_Fig5_HTML.jpg
Figure 8-5.

Building your own project

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.
../images/465467_1_En_8_Chapter/465467_1_En_8_Fig6_HTML.jpg
Figure 8-6.

Using the default Master-Detail App settings

Open the storyboard. You’ll see the two main views shown in Figure 8-7.
../images/465467_1_En_8_Chapter/465467_1_En_8_Fig7_HTML.jpg
Figure 8-7.

Opening the storyboard

Open the library at the top of the utilities pane to see the library objects, as shown in Figure 8-8.
../images/465467_1_En_8_Chapter/465467_1_En_8_Fig8_HTML.jpg
Figure 8-8.

Selecting a bar button item to add

Drag a bar button item to the right of the top bar in the Detail scene, as shown in Figure 8-9.
../images/465467_1_En_8_Chapter/465467_1_En_8_Fig9_HTML.jpg
Figure 8-9.

Adding a bar button item

From the attributes inspector, choose the Action button, as shown in Figure 8-10.
../images/465467_1_En_8_Chapter/465467_1_En_8_Fig10_HTML.jpg
Figure 8-10.

Choosing the action button

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.
../images/465467_1_En_8_Chapter/465467_1_En_8_Fig11_HTML.jpg
Figure 8-11.

Connecting the outlet for actionButton

Add the code for the action, as shown in Figure 8-12.
../images/465467_1_En_8_Chapter/465467_1_En_8_Fig12_HTML.jpg
Figure 8-12.

Preparing the button and action

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 .
@IBAction func actionButtonAction(_ sender: Any) {
  let sharedItems = "Sample text"
  let activityViewController = UIActivityViewController(activityItems:
    [textToShare], applicationActivities: nil)
  // position the popover relative to this view
  activityViewController.popoverPresentationController?.sourceView = self.view
  // present the view controller
  self.present(activityViewController, animated: true,
    completion: nil)
  }
}
Listing 8-1

actionButtonAction

../images/465467_1_En_8_Chapter/465467_1_En_8_Fig13_HTML.jpg
Figure 8-13.

Experimenting with an activity view controller

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.
../images/465467_1_En_8_Chapter/465467_1_En_8_Fig14_HTML.jpg
Figure 8-14.

Using the activity view controller

If you have installed Mail, you can send the text, as shown in Figure 8-15 .
../images/465467_1_En_8_Chapter/465467_1_En_8_Fig15_HTML.jpg
Figure 8-15.

You can email text from the activity view controller

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:
init(activityItems: [Any], applicationActivities: [UIActivity]?)

You’ll notice that you can specify activities as an optional; there is more information on them in the documentation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.84.32