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.

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
    )