Making files from your app available in the iOS Files app

Learn how and why you can make your app's Documents folder accessible via Files app in iOS. And also how to write files into the Application Support directory.

Published: Nov. 22, 2020
I want a Press Kit

If you ever needed to store a file in your iOS app, chances are, you used the documents directory available for your app. Maybe with code like this:

lazy var documentsURL: URL = {
        let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return urls[0]
 }()

How

Did you know you can make this directory accessible to users via the Files app? It takes just a little bit of configuration. This consists of adding two keys to Info.plist and these are:

UIFileSharingEnabled

LSSupportsOpeningDocumentsInPlace

Both need to be set to "YES".

Once you enable this, you can open Files app, navigate to the "On My iPhone" section and if you have any files inside Documents directory, you will se directory with your app name with these files inside.

Why

So that is the how done. The why question is more complicated. I think exposing files like this makes sense for a particular types of apps. If maybe you have something like a Markdown editor, then it may make sense to make these files available to the users, so they can easily copy them or open them in another app.

Or maybe you could expose backups in this way. You need to keep in mind that user may delete or edit these files and you app should react accordingly. While working on this post I noticed that app REC for video recording uses this directory to save videos that were taken with the app but user (in this case me), did not choose to save them to Photos.

Similarly photo editing app Darkroom has a public directory with a lot of random files which I suspect is unintended.

Also Documents directory is not the only place to store your app files, just the most common. So you can store the most of the files in other directories outside the users reach and use Documents directory to expose files that may be useful for users to have direct access to.

Writing files into the Application Support directory

So, if you want your Documents to be public and also save some files that should not be public, where to save them? The best place is Application Support directory. This is designed for files that your app needs and should not be accessible to the user.

Getting the URL is pretty straightforward:

lazy var applicationSupportURL: URL = {
        let urls = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)
        return urls[0]
}()

However that is not all. Unlike Documents, this directory is not created by default and if you try to save files there, you get an error telling you that folder does not exist. You need to create it first and then it will work just like Documents folder.

The check for directory is not very pretty code, but I haven't found a better alternative:

var isDir: ObjCBool = true
if !FileManager.default.fileExists(atPath: applicationSupportURL.path, isDirectory: &isDir) {
        do {
            try FileManager.default.createDirectory(atPath: applicationSupportURL.path, withIntermediateDirectories: true, attributes: nil)
        } catch {
            print(error)
        }
}

And done! Application Support directory is now ready for use.

Thanks for reading!

Uses: Xcode 12 & Swift 5.3

Filip Němeček profile photo

WRITTEN BY

Filip Němeček @nemecek_f@iosdev.space

iOS blogger and developer with interest in Python/Django. Want to see most recent projects? 👀

iOS blogger and developer with interest in Python/Django. Want to see most recent projects? 👀