Skip to content

Commit

Permalink
Merge pull request #34 from psuzn/fix-ios-data-dir
Browse files Browse the repository at this point in the history
Use NSApplicationSupportDirectory for all darwin platforms
  • Loading branch information
psuzn authored Apr 23, 2024
2 parents 53f9785 + 7bf649f commit 06f3cfc
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Platform-specific application home and cache directories for KMP.
| Platform | Cache Directory | Data Directory |
|-----------------------|--------------------------------------------|--------------------------------------------|
| Android | `context.cacheDir` | `ApplicationInfo.dataDir` |
| IOS/IpadOs/WatchOs | `NSCachesDirectory` | `NSHomeDirectory` |
| IOS/IpadOs/WatchOs | `NSCachesDirectory` | `NSApplicationSupportDirectory` |
| Mac (native/jvm/node) | `~/Library/Caches/<app-id>` | `~/Library/Application Support/<app-id>` |
| Windows (jvm/node) | `C:\Users\<user>\AppData/Caches/<app-id>` | `C:\Users\<user>\AppData/<app-id>>` |
| Linux (jvm/node) | `~/.cache/<app-id>` | `~/local/share/<app-id>` |
Expand Down Expand Up @@ -58,7 +58,7 @@ val packageName = "example.com.app"
val dataDirectory = appDataDirectory(packageName)
```

This will return `ApplicationInfo.dataDir` on android, `NSHomeDirectory` on IOS and equivalent platform specific data
This will return `ApplicationInfo.dataDir` on android, `NSApplicationSupportDirectory` on IOS and equivalent platform specific data
directory on other platforms.

#### App cache directory
Expand Down
2 changes: 1 addition & 1 deletion multiplatform-paths/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ mavenPublishing {
pom {
name.set("Multiplatform Paths")
description.set(
"Get platform specific app data and cache directory(equivalent to ApplicationInfo.dataDir or NSHomeDirectory) in Kotlin Multiplatform application",
"Get platform specific app data and cache directory(equivalent to ApplicationInfo.dataDir or NSApplicationSupportDirectory) in Kotlin Multiplatform application",
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@ import me.sujanpoudel.utils.platformIdentifier.Platform
import me.sujanpoudel.utils.platformIdentifier.platform
import platform.Foundation.NSApplicationSupportDirectory
import platform.Foundation.NSCachesDirectory
import platform.Foundation.NSHomeDirectory
import platform.Foundation.NSSearchPathForDirectoriesInDomains
import platform.Foundation.NSUserDomainMask

actual fun dataDirectory(appId: String): Path = if (platform() is Platform.OS.MacOs) {
actual fun dataDirectory(appId: String): Path =
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true)
.firstOrNull()?.toString()?.toPath()
?.let { it / appId } ?: error("Unable to get 'NSApplicationSupportDirectory'")
} else {
NSHomeDirectory().toPath()
}

actual fun cacheDirectory(appId: String): Path {
val cachesDirectory = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true)
Expand Down

0 comments on commit 06f3cfc

Please sign in to comment.