iTunes Tutorial for iOS: How To Integrate iTunes File Sharing With Your iOS App

An iTunes tutorial on how to let users easily copy files to and from your app using the iTunes File Sharing feature. By Ray Wenderlich.

Leave a rating/review
Save for later

Freak out your friends by sending them bugs!

Freak out your friends by sending them bugs!

The iPad and iOS 4 have a great new feature called File Sharing that provides a convenient way for users to transfer files between their computer and your app.

But figuring out exactly what you need to do to get this working in real-world scenarios can be tricky. So I thought it would be useful to write an iTunes tutorial that covers how to do that with a real app, step by step!

If you’ve been following the development of the “Scary Bugs” app in the simple app tutorial or NSCoding tutorial, it’s all been leading up to this!

In those tutorials, we’ve been creating an app that allows you to collect scary bugs and rate them. However, it would be even better if users could share bugs with their friends (or mortal enemies!)

So let’s do it! You’ll need a copy of the ScaryBugs project where we last left off – if you don’t have it already, you can grab a copy here.

File Sharing Overview

Let’s start with an overview with how File Sharing works.

To enable File Sharing in your app, you simply set the boolean flag “UIFileSharingEnabled” in your info.plist.

iTunes will then display anything you save to the Documents directory in your app to the user, when they go to the “Apps” page in iTunes and scroll to the bottom:

iTunes File Sharing Screenshot

Of course, for this to be of any use, your app needs to have some smarts in it.

First, your app needs to be able to detect the files that the user puts into this directory.

Second, your app needs to be able to deal with the fact that the contents of this folder can change at any time. The user can rename files, delete files, or even put garbage in there if they want.

Third, your app needs to have a way to export your app’s document as a single file, even if it consists of multiple files.

“But wait!”, you may say. “What about packages, can’t I just set up my app to use a Document Package and have my folder of files be treated like a single file?”

Well, about that…

File Sharing and Document Packages

If you want to skip this section and move on, the TLDR summary is: Document Packages don’t work the way you’d expect with iTunes currently, so you need to use an alternative solution.

But if you’re curious about them and why they don’t work, here’s some more information.

For those of you who are unfamiliar, packages are a way that iOS and MacOS can treat a directory of files as a single file. This is used for .apps – these are actually directories of files, with a standard structure (a “bundle”).

Many apps on the Mac use store their documents as packages – Document Packages to be specific. Again, these are simply directories with whatever makes up the app’s document contents inside, often with a particular file extension.

You can register your app as the owner of files with a particular extension in your info.plist, and mark the type as a package with the LSTypeIsPackage key. If you do this on the Mac and install your app to your Applications folder, the OS will detect it and start treating any directory with your registered extension as a package.

You can register your iOS app as being the owner of a file type/extension that is a package as well. If you save it to the Documents directory, you might think that it will show up in File Sharing as a single file rather than a directory.

Well it does – but it doesn’t work exactly the way you’d think. It will show up in iTunes as a single file just fine:

File Sharing Document Packages from iPhone

However, when you save the package to disk, it will be saved as a folder, not as a single file:

File Sharing Document Packages from iPhone on Mac

To make it worse, there’s no way for the user to import the folder back into iTunes, and they can mess around with the contents of the folder.

You could get the package to show up as a single file if you also had a companion app installed on the Mac, but most of the time as app developers we won’t be able to guarantee that the user has the companion app installed as well. Plus, this wouldn’t work at all on a PC.

So as I said, document packages just aren’t the way to go with File Sharing at this point. “But wait!”, you may say. “What about Pages and other Apple iPad apps, don’t they use packages just fine?”

Well, about that…

File Sharing and Pages

If you’re curious how Apple handles things, if you don’t have Pages you might want to check out this great article with a summary of the File Sharing process with Pages.

But as a quick summary, here’s how I understand Pages to work:

  • The Pages documents aren’t actually packages, they are zipped packages so they can be treated as a single file.
  • Pages actually has two separate lists of documents:
    • The list of documents that Pages uses, in a private directory not available to the user.
    • The list of documents that is available for File sharing, in the Documents directory.
  • The user has to actually take a step to import or export a document to/from File Sharing (it doesn’t just automatically show up). I believe this is because it would be a performance penalty to constantly create zipped copies of documents mirroring the actual documents.
  • The list of documents that Pages uses, in a private directory not available to the user.
  • The list of documents that is available for File sharing, in the Documents directory.

So, in our case with Scary Bugs, we are in a similar situation since we have documents made up with several files. Therefore, we are going to take Apple’s approach, and have manual steps to import/export files to File Sharing, and export our documents as zipped directories.