zacharee / KMPFile

Kotlin Multiplatform File implementation for JVM, Android, and iOS
MIT License
21 stars 0 forks source link

IOS real device run failed,Operation not permitted #4

Closed dorkytiger closed 10 hours ago

dorkytiger commented 4 days ago

I tried running it separately on the iOS simulator and the real iOS device. The simulator had no issues, but the real device reported an error saying :

kotlinx.io.IOException: Failed to open /private/var/mobile/Containers/Shared/AppGroup/A360F614-B4FB-4359-926D-E5ABC17BB1F2/File Provider Storage/_xxx.pdf with Operation not permitted

Did I miss setting any permissions? Below is the code I ran:

  @OptIn(ExperimentalForeignApi::class, BetaInteropApi::class)
    actual fun getPdfPageCount(path: String): Int =
        kotlin.runCatching {
            val pdfFile = FileUtils.fromString(path, isDirectory = false) ?: return 0
            val data = pdfFile.openInputStream()?.readByteArray() ?: return 0
            val nsData = data.usePinned {
                NSData.create(bytes = it.addressOf(0), length = data.size.toULong())
            }
            val pdfDocument = PDFDocument(nsData)
            return pdfDocument.pageCount.toInt()
        }.onFailure {
            it.printStackTrace()
        }.getOrDefault(0)
zacharee commented 4 days ago

Where are you obtaining that path?

dorkytiger commented 2 days ago

I use this function get the path,because I want to save the file info in local database, and when I need it I convert it to IPlatformFile

    actual fun getAbsolutePath(file: IPlatformFile): String {
        return file.getAbsolutePath()
    }
zacharee commented 2 days ago

But where are you getting this file from? What's the source?

dorkytiger commented 2 days ago

But where are you getting this file from? What's the source?

this,i get from it

        val filePicker = rememberFilePickerLauncher(
            type = PickerType.File(listOf("pdf")),
            mode = PickerMode.Single,
            title = "Select a PDF file",
        ) { pdfFile ->
            scope.launch {
                withContext(Dispatchers.IO) {
                    pdfFile?.toKmpFile()?.let {
                        viewModel.onAction(
                            UploadFormAction.CreateUploadTask(
                                UploadEntity(
                                    userId = user?.userId ?: 0,
                                    type = selectFileType,
                                    skipSameNameFolder = skipSameNameFolder,
                                    libraryId = selectLibrary?.libraryId ?: 0,
                                    pathUrl = FileUtil.getAbsolutePath(it),
                                    title = it.getName()
                                )
                            )
                        )
                        navigator.dispose(UploadScreen())
                        navigator.push(UploadScreen())
                    }
                }
            }
        }

        val directoryPicker = rememberDirectoryPickerLauncher(
            title = "Select a Directory"
        ) { directory ->
            scope.launch {
                withContext(Dispatchers.IO) {
                    directory?.toKmpFile()?.let {
                        viewModel.onAction(
                            UploadFormAction.CreateUploadTask(
                                UploadEntity(
                                    title = it.getName(),
                                    userId = user?.userId ?: 0,
                                    type = selectFileType,
                                    skipSameNameFolder = skipSameNameFolder,
                                    libraryId = selectLibrary?.libraryId ?: 0,
                                    pathUrl = FileUtil.getAbsolutePath(it),
                                )
                            )
                        )
                        navigator.dispose(UploadScreen())
                        navigator.push(UploadScreen())
                    }
                }
            }
        }
zacharee commented 2 days ago

Can you try 0.6.3 once it's published?

dorkytiger commented 2 days ago

Can you try 0.6.3 once it's published?

Thinks! of course i will try it ,if it fix, i will tell you

dorkytiger commented 10 hours ago

Can you try 0.6.3 once it's published?

it work!thank you for your help!