diff --git a/Sources/AppState/Application/Application+public.swift b/Sources/AppState/Application/Application+public.swift index c1399cc..cd5172a 100644 --- a/Sources/AppState/Application/Application+public.swift +++ b/Sources/AppState/Application/Application+public.swift @@ -1070,7 +1070,7 @@ public extension Application { */ func fileState( initial: @escaping @autoclosure () -> Value, - path: String = "~/App", + path: String = FileManager.defaultFileStatePath, filename: String ) -> FileState { FileState( @@ -1088,7 +1088,7 @@ public extension Application { - Returns: The state of type `Value`. */ func fileState( - path: String = "~/App", + path: String = FileManager.defaultFileStatePath, filename: String ) -> FileState { fileState( diff --git a/Sources/AppState/Application/Types/Helper/FileManager+AppState.swift b/Sources/AppState/Application/Types/Helper/FileManager+AppState.swift index 9025ca8..9518e38 100644 --- a/Sources/AppState/Application/Types/Helper/FileManager+AppState.swift +++ b/Sources/AppState/Application/Types/Helper/FileManager+AppState.swift @@ -7,6 +7,22 @@ extension FileManager { var errorDescription: String? { rawValue } } + public static var defaultFileStatePath: String { + guard let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { + return "~/App" + } + + #if !os(Linux) && !os(Windows) + if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) { + return "\(path.path())/App" + } else { + return "\(path.path)/App" + } + #else + return "\(path.path)/App" + #endif + } + /// Creates a directory at the specified path. /// /// - Parameters: diff --git a/Tests/AppStateTests/FileStateTests.swift b/Tests/AppStateTests/FileStateTests.swift index 734f015..8e2ae89 100644 --- a/Tests/AppStateTests/FileStateTests.swift +++ b/Tests/AppStateTests/FileStateTests.swift @@ -11,7 +11,7 @@ fileprivate extension Application { } var storedString: FileState { - fileState(path: "./AppStateTests", filename: "storedstring") + fileState(filename: "storedstring") } }