Reputation: 145
I'm using KMM library in my project. I want to access local json files in both iOS and Android from shared resources. I don't want to use files from iOS App. I have written following code in shared/src/commonMain/iOSMain but its not working
val bundle = NSBundle.mainBundle
val path = bundle.pathForResource(name = "sample2", ofType = "json") ?: "File not found"
I have put my json files in commonMain/resources/sample2.json
I have also try by putting it in commonMain/resources/iOSMain/sample2.json Still its not working.
Any suggestions?
We have also tried using compose resources inside CommonMain folder depicted in official JetBrains website but not working for iOS
bytes = Res.readBytes("files/$locale.json").decodeToString()
Upvotes: 2
Views: 273
Reputation: 7989
This is possible using Compose Multiplatform Resources, but everything has to be configured exactly. This is how I'm reading a text file on Android, iOS, and Desktop:
commonMain/composeResources/files/words.txt
:compose.components.resources
is added to commonMain
dependencies: @OptIn(ExperimentalResourceApi::class)
suspend fun readWords(): Map<String, List<Word>> {
val content = Res.readBytes("files/words.txt").decodeToString()
Upvotes: 0