From b8468ee85c82d6a7c8a0b5dbcc08efa1f6cfc08f Mon Sep 17 00:00:00 2001 From: Joe Fabisevich Date: Thu, 24 Aug 2023 10:39:55 -0400 Subject: [PATCH] Using URL static properties for directories on iOS 16/macOS 13 --- Sources/Bodega/FileManager.Directory.swift | 47 ++++++++++++++++------ 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/Sources/Bodega/FileManager.Directory.swift b/Sources/Bodega/FileManager.Directory.swift index 5336f0d..2b6ae9a 100644 --- a/Sources/Bodega/FileManager.Directory.swift +++ b/Sources/Bodega/FileManager.Directory.swift @@ -27,25 +27,40 @@ public extension FileManager.Directory { /// Returns a URL to a subfolder created in the documents directory based on the `pathComponent`. /// - Parameter pathComponent: A path to append to the platform's documents directory. static func documents(appendingPath pathComponent: String) -> Self { - self.init( - url: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(pathComponent) - ) + let url: URL + if #available(iOS 16.0, macOS 13.0, *) { + url = URL.documentsDirectory.appending(path: pathComponent) + } else { + url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(pathComponent) + } + + return self.init(url: url) } /// Returns a URL to a subfolder created in the caches directory based on the `pathComponent`. /// - Parameter pathComponent: A path to append to the platform's caches directory. static func caches(appendingPath pathComponent: String) -> Self { - self.init( - url: FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent(pathComponent) - ) + let url: URL + if #available(iOS 16.0, macOS 13.0, *) { + url = URL.cachesDirectory.appending(path: pathComponent) + } else { + url = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent(pathComponent) + } + + return self.init(url: url) } /// Returns a URL to a subfolder created in the temporary directory based on the `pathComponent`. /// - Parameter pathComponent: A path to append to the platform's temporary directory. static func temporary(appendingPath pathComponent: String) -> Self { - self.init( - url: FileManager.default.temporaryDirectory.appendingPathComponent(pathComponent) - ) + let url: URL + if #available(iOS 16.0, macOS 13.0, *) { + url = URL.temporaryDirectory.appending(path: pathComponent) + } else { + url = FileManager.default.temporaryDirectory.appendingPathComponent(pathComponent) + } + + return self.init(url: url) } /// For apps that use the App Groups feature this function returns a URL that @@ -61,12 +76,18 @@ public extension FileManager.Directory { ) } - @available(macOS 11, *) /// Returns a URL to a subfolder created in the Application Support directory based on the `pathComponent`. /// - pathComponent: A path to append to the app's group shared directory. +#if os(macOS) static func applicationSupport(appendingPath pathComponent: String) -> Self { - self.init( - url: FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!.appendingPathComponent(pathComponent) - ) + let url: URL + if #available(macOS 13.0, *) { + url = URL.applicationSupportDirectory.appending(path: pathComponent) + } else { + url = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!.appendingPathComponent(pathComponent) + } + + return self.init(url: url) } +#endif }