# Use new URL related APIs from iOS 16 in lower versions

Natalia Panferova wrote an excellent [article](https://nilcoalescing.com/blog/GetURLsForSystemFolders/) about how working with files and directories got much more straightforward in iOS 16.

For example, we can get the paths to system folders directly from static properties on the `URL` type instead of using `FileManager` APIs.

```swift
if #available(iOS 16.0, *) {
    let docDir = URL.documentsDirectory
}
```

However, most of us are not ready to increase the minimum deployment target of our apps / frameworks. This forces us

- to write compiler directives when we want to leverage new APIs
- to provide alternative code when running on devices with lower OS versions

Here is an example that uses the new `appending(path:directoryHint:)` method and its `InferFromPath` default behavior.

```swift
let fileURL: URL

if #available(iOS 16.0, *) {
    fileURL = URL
        .documentsDirectory
        .appending(path: "myfile")
} else {
    fileURL = try FileManager.default
        .url(
            for: .documentDirectory,
            in: .userDomainMask,
            appropriateFor: nil,
            create: false
        )
        .appendingPathComponent(
            "myfile", isDirectory: false
        )
}
```

**Writing conditional compiler directives and compatible code is cumbersome, so I did it for you!** I created [`URLCompatibilityKit`](https://github.com/MarcoEidinger/URLCompatibilityKit), a lightweight Swift package that adds compatible backports of commonly used type properties, type and instance properties for URL that are only available from iOS 16.0+ / macOS 13.0+ / tvOS 16.0+ / watchOS 9.0+.

Once you added the Swift Package and used an `import URLCompatibilityKit` statement, you can leverage those iOS 16 goodies.

```swift
import URLCompatibilityKit

// works on devices running < iOS 16
let fileURL =  URL.documentsDirectory.appending(path: "myfile")
```

You can remove the Swift Package and related import statements once you are ready to change the minimum deployment target of your app / framework to iOS 16. 

%[https://github.com/MarcoEidinger/URLCompatibilityKit]

