Better way to get paths to system directories in iOS 16

⋅ 3 min read ⋅ iOS iOS 16

Table of Contents

In iOS, we have many directories that our app can access, e.g., document directory and cache directory.

Before iOS 16, we can get the URL to these directories with the help of FileManager.

Here is an example of how we get a URL to document directory, a folder where we can keep your app's data.

You might have seen either of these two variations.

// 1
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths.first

// 2
let documentsDirectory = try? FileManager.default.url(
for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)

The problems of getting path with FileManager

Both file manager's methods above have the same problems.

They are prone to error and verbose.

You have to specified both

  • directory: FileManager.SearchPathDirectory and
  • domain: FileManager.SearchPathDomainMask for the path you are looking for.

And if a combination of those two doesn't possible.

The following example would return nil URL.

let invalidCombination = FileManager.default.urls(
for: .desktopDirectory,
in: .networkDomainMask).first
// nil

let invalidCombination = try? FileManager.default.url(
for: .desktopDirectory,
in: .networkDomainMask,
appropriateFor: nil,
create: false)
// nil

Those two methods allow us to reach a wide range of URLs which might be beneficial for a system with more flexible, e.g., macOS.

But in iOS, everything is more restricted. Most of the time, you don't want that flexibility.

You want an easy and concise way to reference subset of paths, e.g., user's document directory, user's cache directory. And that is made possible in iOS 16.

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Getting a system directory path in iOS 16

In iOS 16, URL got a whole pack of type properties that reference a different path within a user domain.

  • static var applicationDirectory: URL
  • static var applicationSupportDirectory: URL
  • static var cachesDirectory: URL
  • static var desktopDirectory: URL
  • static var documentsDirectory: URL
  • static var downloadsDirectory: URL
  • static var homeDirectory: URL
  • static var libraryDirectory: URL
  • static var moviesDirectory: URL
  • static var musicDirectory: URL
  • static var picturesDirectory: URL
  • static var sharedPublicDirectory: URL
  • static var temporaryDirectory: URL
  • static var transferRepresentation: TransferRepresentation
  • static var trashDirectory: URL
  • static var userDirectory: URL

They are convenient properties that get the same result as the existing method, but I think it convey a clearer intention and are also easy to remember and use.

You get it right away what the code is trying to do.

let documentsDirectory = URL.documentsDirectory

// Compare to this
let documentsDirectory = try? FileManager.default.url(
for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Conclusion

The way that we retrieve URLs to various user paths before iOS 16 isn't straightforward. The syntax is verbose and error-prone.

It can confuse newcomers, and hard to get it right, even for experienced ones.

I think these new type properties are a nice improvement to the system.


Read more article about iOS, iOS 16, or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
How to change SwiftUI list background color

In iOS 16, we finally got a native way to change the background color of a list view in SwiftUI.

Next
UIPasteBoard's privacy change in iOS 16

In iOS 16, Apple allows users to grant or deny pasteboard reading permission before it happens. Let's see what that means to users.

← Home