Last active
November 21, 2023 20:01
-
-
Save shakir915/ada27a169bdbb3549bc1aa37a677d30d to your computer and use it in GitHub Desktop.
file io in IOS kotlin native KMM / compose multiplatform file read write in ios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// file io in IOS kotlin native KMM / compose multiplatform file read write in ios | |
//common main | |
expect class PlatformFileWriter() { | |
fun read(filename: String): String? | |
fun save(filename: String, content: String) | |
fun append(filename: String, content: String) | |
} | |
//ios main | |
@file:OptIn(ExperimentalForeignApi::class, ExperimentalForeignApi::class) | |
import kotlinx.cinterop.ExperimentalForeignApi | |
import platform.Foundation.NSDocumentDirectory | |
import platform.Foundation.NSFileHandle | |
import platform.Foundation.NSSearchPathForDirectoriesInDomains | |
import platform.Foundation.NSString | |
import platform.Foundation.NSUTF8StringEncoding | |
import platform.Foundation.NSUserDomainMask | |
import platform.Foundation.closeFile | |
import platform.Foundation.dataUsingEncoding | |
import platform.Foundation.fileHandleForWritingAtPath | |
import platform.Foundation.seekToEndOfFile | |
import platform.Foundation.stringByAppendingPathComponent | |
import platform.Foundation.stringWithContentsOfFile | |
import platform.Foundation.writeData | |
import platform.Foundation.writeToFile | |
actual class PlatformFileWriter actual constructor() { | |
actual fun read( filename: String) : String? { | |
val documentDirectory = NSSearchPathForDirectoriesInDomains( | |
NSDocumentDirectory, | |
NSUserDomainMask, | |
true | |
)[0] as NSString | |
// Append the file name to the document directory path | |
val filePath = documentDirectory.stringByAppendingPathComponent(filename) | |
return NSString.stringWithContentsOfFile(filePath, NSUTF8StringEncoding, null) | |
} | |
actual fun save( filename : String, content : String) { | |
val documentDirectory = NSSearchPathForDirectoriesInDomains( | |
NSDocumentDirectory, | |
NSUserDomainMask, | |
true | |
)[0] as NSString | |
// Append the file name to the document directory path | |
val filePath = documentDirectory.stringByAppendingPathComponent(filename) | |
(content as NSString).writeToFile(filePath, true, NSUTF8StringEncoding, null) | |
} | |
actual fun append(filename: String, content: String) { | |
val documentDirectory = NSSearchPathForDirectoriesInDomains( | |
NSDocumentDirectory, | |
NSUserDomainMask, | |
true | |
)[0] as NSString | |
// Append the file name to the document directory path | |
val filePath = documentDirectory.stringByAppendingPathComponent(filename) | |
// Open the file in append mode | |
val fileHandle = NSFileHandle.fileHandleForWritingAtPath(filePath) ?: run { | |
// If the file doesn't exist, create it | |
(content as NSString) .writeToFile(filePath, true, NSUTF8StringEncoding, null) | |
return | |
} | |
// Move to the end of the file | |
fileHandle.seekToEndOfFile() | |
// Convert NSString to NSData | |
val data = (content as NSString).dataUsingEncoding(NSUTF8StringEncoding)!! | |
// Write content to the end of the file | |
fileHandle.writeData(data) | |
// Close the file handle | |
fileHandle.closeFile() | |
} | |
} | |
//usage | |
PlatformFileWriter().save("textFile.txt","Lorem ipsum") | |
PlatformFileWriter().read("textFile.txt",) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment