UIActivityViewController Tutorial: Sharing Data

In this UIActivityViewController tutorial, you’ll learn all about giving your users the ability to export their data and share it with others. By Owen L Brown.

Leave a rating/review
Download materials
Save for later
You are currently viewing page 2 of 2 of this article. Click here to view the first page.

Exporting App Data

So far in this UIActivityViewController tutorial, you’ve added functionality to your app that handles importing data from other apps. However, what if you want to share your app’s data?

You’re in luck, as Apple has made exporting data nice and easy.

Your app will need code to handle exporting your favorite books. Open Book.swift and replace the existing exportToURL() definition with the following:

func exportToURL() -> URL? {
  // 1
  guard let encoded = try? JSONEncoder().encode(self) else { return nil }
  // 2
  let documents = FileManager.default.urls(
    for: .documentDirectory,
    in: .userDomainMask
  guard let path = documents?.appendingPathComponent("/\(name).btkr") else {
    return nil
  // 3
  do {
    try encoded.write(to: path, options: .atomicWrite)
    return path
  } catch {
    return nil

Here’s a step-by-step explanation of the code above:

  1. Encode Book as JSON.
  2. Verify that your app can access its Documents directory without error.
  3. Finally, save the data to your Documents directory and return the URL of the newly-created file.

Now that you can export Book data to a file, you’re going to need an easy way to share that data. Open BookDetailViewController.swift and replace the implementation of share(_:) with the following:

@IBAction func share(_ sender: UIBarButtonItem) {
  // Get latest changes
  // 1
    let detailBook = detailBook,
    let url = detailBook.exportToURL() 
    else { return }
  // 2
  let activity = UIActivityViewController(
    activityItems: ["Check out this book! I like using Book Tracker.", url],
    applicationActivities: nil
  activity.popoverPresentationController?.barButtonItem = sender

  // 3
  present(activity, animated: true, completion: nil)

Here’s a step-by-step explanation of the code above:

  1. Ferify that you have a detail book and that you can retrieve the URL from exportToURL().
  2. Create an instance of UIActivityViewController and, for activityItems, pass in a message and the URL to your app’s data file. Depending on the activity the user selects from the activity menu, iOS uses the message you pass in to pre-populate its content. For example, if the user chooses to share the book via Email, the message you passed in will pre-populate the body of the email. Since not all activities have such capability, such as AirDrop, iOS may discard the message. UIActivityViewController will do all of this heavy lifting for you and, since you defined all of the required information in your Info.plist, it’ll know just what to do with it.
  3. Present the UIActivityViewController to the user.

Build and run your app, open a book and try to share it. You’ll notice that you see a few options available to you as shown below:

UIActivityViewController tutorial book sharing

UIActivityViewController has been around since iOS6, but despite how useful it is, it’s often under-appreciated. Perhaps one of the best options is the ability to AirDrop. If you have two devices — or better yet, a friend with the Book Tracker app installed — you can test AirDropping books!

Where to Go From Here?

You can download the completed version of the project using the Download Materials button at the top or bottom of this tutorial.

Now that you know how iOS imports and exports app data, you’re ready to take this knowledge and start sharing your favorite books or any other data of your choice.

Here are some resources for further study: