Avadhut
12/09/2023, 6:08 PMraw
directory, and then it can be accessed as a normal file.
What's the equivalent approach for handling such static files in shared KMP code?
Thanks!Raed Ghazal
12/09/2023, 6:43 PMRaed Ghazal
12/09/2023, 6:44 PMRaed Ghazal
12/09/2023, 6:49 PMassets
directory, so I accessed it normally from android, then added it to the iosApp directory and accessed it in iosMain natively as well, let me share the snippetsRaed Ghazal
12/09/2023, 6:50 PMimport kotlinx.cinterop.ExperimentalForeignApi
import platform.Foundation.NSBundle
import platform.Foundation.NSString
import platform.Foundation.NSUTF8StringEncoding
import platform.Foundation.stringWithContentsOfFile
actual class AssetsReader {
@OptIn(ExperimentalForeignApi::class)
actual fun readFile(fileName: String): String {
val path = NSBundle.mainBundle.pathForResource("data", ofType = "json")
?: throw Exception("data.json File not found")
return NSString.stringWithContentsOfFile(path, NSUTF8StringEncoding, null)
?: throw Exception("failed to encode data.json file")
}
}
Avadhut
12/09/2023, 6:52 PMRaed Ghazal
12/09/2023, 6:54 PMRaed Ghazal
12/09/2023, 6:54 PMRaed Ghazal
12/09/2023, 6:54 PMimport android.content.Context
actual class AssetsReader constructor(private val context: Context) {
actual fun readFile(fileName: String): String {
val file = context.assets.open("quran_data.json")
return file.bufferedReader().readText()
}
}
Raed Ghazal
12/09/2023, 6:55 PMAvadhut
12/09/2023, 6:56 PMAvadhut
12/09/2023, 6:56 PMMcEna
12/10/2023, 9:18 AM