Universal Type Identifiers Tutorial for iOS: Importing and Exporting App Data

In this tutorial, you’ll learn how to export and import app data to and from your iOS app, as well as create custom file types and extensions. By Ehab Amer.

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

Excluding Activity Types

You have sharing enabled now, but what if you want to disable sharing for certain channels? Well, UIActivityViewController allows you to do just that by specifying which system-known share activities to exclude.

To exclude certain activities, use excludedActivityTypes and provide it with the UIActivity.ActivityType enum values you want to exclude. Some common examples are .postToFacebook, .airDrop and .copyToPasteboard.

Give it a shot. In ContentView.swift, enter .copyToPasteboard to the array passed in to excludedActivityTypes.

ShareSheet(
  activityItems: [TaskStore.shared.tasksDocURL],
  excludedActivityTypes: [.copyToPasteboard])

Build and run and tap on the Share button to see the difference.

share sheet without and with excluded types

Notice that the Copy option is no longer available.

One drawback you should know about is that you can only exclude types that the system recognizes. This means that if an app like this one is able to import your file, you won’t be able to exclude it. Only the types defined in the UIActivity.ActivityType enum can be excluded.

Exporting Files via Email

What if you want to export the information through email? And what if you want to provide the email recipient, subject and text body to make it simple for the user to send it? This is a common scenario when users send bug logs to a support email address.

To do this, you’ll use MFMailComposeViewController. You can specify everything mentioned above, add attachments and have a proper email ready for the user to send with the push of a button. The only thing you can’t do is tap the button on the user’s behalf. :] You wouldn’t want apps sending emails on your behalf; Apple made sure they can’t.

Note: The next part requires a device with an email account already set up on it. MFMailComposeViewController will crash if you attempt to present it without having an email account configured. If you set up one on the simulator, it’ll still complain.

In ContentView.swift, add this right before the Share button you created earlier.

// Export Via Email
Button(action: { self.mailViewIsPresented = true }) {
  Image(systemName: "envelope")
}
.frame(width: 44, height: 44, alignment: .center)
.disabled(!MFMailComposeViewController.canSendMail())
.sheet(isPresented: $mailViewIsPresented) {
  MailView(
    messageBody: "This is a test email string",
    attachmentInfo: nil,
    result: self.$result)
}

This code creates a new button, which when tapped, will create an MFMailComposeViewController using the MailView class already provided in the project.

Build and run the app on your device. Tap on the new envelope button and you’ll see an email draft with the text, “This is a test email string“, already in the body. Pretty cool, eh?

App created email draft with predetermined text

Adding an Attachment

Now that you have the email form ready, you’re ready to add the final ingredient: the app’s data.

MFMailComposeViewController‘s addAttachmentData(_:mimeType:fileName:) can add as many attachments to your email as you need. You need to provide it with the file’s Data, its MIME type and the desired file name.

In ContentView.swift, replace MailView(messageBody:attachmentInfo:result:) from the previous step with:

MailView(
  messageBody: "This is a test email string",
  attachmentInfo: (
    fileURL: TaskStore.shared.tasksDocURL,
    mimeType: "application/xml"),
    result: self.$result)

Build and run and tap the envelope button. You’ll see the same email but this time with an attachment named ExportData.rwtl. The file itself will show the icon you specified for that file type.

Email with body and attachment

Understanding the MIME Type

In the code above, you provided the MIME type for your file. What is this, you ask? Well, MIME stands for Multipurpose Internet Mail Extensions. Think of it as a description for raw data inside the email. When the recipient’s email client parses this email, it’ll use that value to know how to parse the contents and attachments of the email correctly.

In this case, an rwtl file is a plist file with a different extension. A plist file is an XML file in nature, which explains the second part. And the first part, application, indicates that this type is only readable by apps and not users.

You can now export a list of tasks from your app, send it to your friend and have them import it into their own copy of your app!

Where to Go From Here?

You can download the completed project files by clicking on the Download Materials button at the top or bottom of the tutorial.

This tutorial showed you how to import app data into your data source. But currently, the import process replaces all the existing information with the newly imported data. This might not always be the desired functionality. You might want to compare the old with the new and possibly merge them.

If so, take a look at these two articles that will show you how you can preview files and even edit them in place without making a copy to your app: Document-Based Apps Tutorial: Getting Started and Apple’s Documentation.

And if you want to learn more about UTIs, take a look at these articles from Apple: Uniform Type Identifiers Overview and Uniform Type Identifiers Reference

I hope you enjoyed this tutorial! If you have any questions or comments, please join the forum discussion below. :]