Get URLs for system folders in iOS 16

Working with files and directories got a lot easier in iOS 16. Previously, to obtain the path to a file in the application folder, we had to use FileManager APIs, which are quite cumbersome. Now, we can get the paths to system folders directly from static properties on URL type.

For example, to get the URL of the documents directory in our application's sandbox, we can read the URL.documentsDirectory property.

let url = URL.documentsDirectory

We can obtain the URLs to other system folders just as easily. Check out the type properties in documentation for URL struct for the full list of available options.

Swift Gems by Natalia Panferova book coverSwift Gems by Natalia Panferova book cover

Level up your Swift skills!$35

100+ tips to take your Swift code to the next level

Swift Gemsby Natalia Panferova

  • Advanced Swift techniques for experienced developers bypassing basic tutorials
  • Curated, actionable tips ready for immediate integration into any Swift project
  • Strategies to improve code quality, structure, and performance across all platforms

Level up your Swift skills!

100+ tips to take your Swift code to the next level

Swift Gems by Natalia Panferova book coverSwift Gems by Natalia Panferova book cover

Swift Gems

by Natalia Panferova

$35

Another great improvement is the new appending(path:directoryHint:) method, that replaces the old appendingPathComponent(_:isDirectory:). The new method accepts a DirectoryHint parameter, which indicates whether Foundation should perform a file system check to determine if the path refers to a folder. The default value is inferFromPath, meaning that this information will be inferred from the path string we pass to the method. If the string contains a trailing slash, it will be treated as a directory path.

Before iOS 16, we either had to provide isDirectory Boolean value to appendingPathComponent(_:isDirectory:) or call appendingPathComponent(_:) without the Boolean parameter. The latter could result in Foundation framework performing a file system operation to determine if the path component is a directory. This has performance implications and can be easily avoided with the new method.

Now to construct a file URL, we can simply write url.appending(path: "myfile"). Foundation will infer from the path that it's not a directory, because it doesn't have a trailing slash, and it won't be doing the unnecessary file system check.

We can get a URL for a file in documents directory in iOS 16 with a very simple line of code, thanks to all of these improvements.

// iOS 16 example
let fileURL = URL.documentsDirectory
    .appending(path: "myfile")

It's an exciting update from iOS 15, where to achieve the same result, we would need to use more verbose FileManager APIs.

// iOS 15 example
let fileURL = try FileManager.default
    .url(
        for: .documentDirectory,
        in: .userDomainMask,
        appropriateFor: nil,
        create: false
    )
    .appendingPathComponent(
        "myfile", isDirectory: false
    )


If you're an experienced Swift developer looking to learn advanced techniques, check out my new book Swift Gems. It’s packed with tips and tricks focused solely on the Swift language and Swift Standard Library. From optimizing collections and handling strings to mastering asynchronous programming and debugging, "Swift Gems" provides practical advice that will elevate your Swift development skills to the next level. Grab your copy of Swift Gems and let's explore these advanced techniques together.