From 7d0bf7b831b1ec14037db687f0e53e2998d5b2a3 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Tue, 27 Aug 2024 16:02:36 +0200 Subject: [PATCH] Fix a resource reading on iOS 12 (#5123) Use deprecated `seekToFileOffset` on iOS 12 to fix a crash on old Apple devices Fixes https://youtrack.jetbrains.com/issue/CMP-5967 ## Release Notes ### Fixes - Resources - _(prerelease fix)_ Fix a resource reading on iOS 12 --- .../compose/resources/ResourceReader.ios.kt | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/components/resources/library/src/iosMain/kotlin/org/jetbrains/compose/resources/ResourceReader.ios.kt b/components/resources/library/src/iosMain/kotlin/org/jetbrains/compose/resources/ResourceReader.ios.kt index 0da7725bc3..5629c5bf2d 100644 --- a/components/resources/library/src/iosMain/kotlin/org/jetbrains/compose/resources/ResourceReader.ios.kt +++ b/components/resources/library/src/iosMain/kotlin/org/jetbrains/compose/resources/ResourceReader.ios.kt @@ -1,10 +1,29 @@ package org.jetbrains.compose.resources +import kotlinx.cinterop.BetaInteropApi +import kotlinx.cinterop.ObjCObjectVar import kotlinx.cinterop.addressOf +import kotlinx.cinterop.alloc +import kotlinx.cinterop.memScoped +import kotlinx.cinterop.ptr import kotlinx.cinterop.usePinned -import platform.Foundation.* +import kotlinx.cinterop.value +import org.jetbrains.skiko.OS +import org.jetbrains.skiko.OSVersion +import org.jetbrains.skiko.available +import platform.Foundation.NSBundle +import platform.Foundation.NSData +import platform.Foundation.NSError +import platform.Foundation.NSFileHandle +import platform.Foundation.NSFileManager +import platform.Foundation.NSURL +import platform.Foundation.closeFile +import platform.Foundation.fileHandleForReadingAtPath +import platform.Foundation.readDataOfLength +import platform.Foundation.seekToFileOffset import platform.posix.memcpy +@OptIn(BetaInteropApi::class) internal actual fun getPlatformResourceReader(): ResourceReader = object : ResourceReader { override suspend fun read(path: String): ByteArray { val data = readData(getPathInBundle(path)) @@ -30,7 +49,15 @@ internal actual fun getPlatformResourceReader(): ResourceReader = object : Resou private fun readData(path: String, offset: Long, size: Long): NSData { val fileHandle = NSFileHandle.fileHandleForReadingAtPath(path) ?: throw MissingResourceException(path) - fileHandle.seekToOffset(offset.toULong(), null) + if (available(OS.Ios to OSVersion(major = 13))) { + memScoped { + val error = alloc>() + fileHandle.seekToOffset(offset.toULong(), error.ptr) + error.value?.let { err -> error(err.localizedDescription) } + } + } else { + fileHandle.seekToFileOffset(offset.toULong()) + } val result = fileHandle.readDataOfLength(size.toULong()) fileHandle.closeFile() return result