diff --git a/calf-file-picker/src/iosMain/kotlin/com.mohamedrejeb.calf.picker/FilePickerLauncher.ios.kt b/calf-file-picker/src/iosMain/kotlin/com.mohamedrejeb.calf.picker/FilePickerLauncher.ios.kt index c050c0d..a6342d9 100644 --- a/calf-file-picker/src/iosMain/kotlin/com.mohamedrejeb.calf.picker/FilePickerLauncher.ios.kt +++ b/calf-file-picker/src/iosMain/kotlin/com.mohamedrejeb.calf.picker/FilePickerLauncher.ios.kt @@ -3,6 +3,7 @@ package com.mohamedrejeb.calf.picker import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.ui.interop.LocalUIViewController import com.mohamedrejeb.calf.core.InternalCalfApi import com.mohamedrejeb.calf.io.KmpFile import kotlinx.cinterop.BetaInteropApi @@ -25,6 +26,7 @@ import platform.PhotosUI.PHPickerViewControllerDelegateProtocol import platform.UIKit.UIApplication import platform.UIKit.UIDocumentPickerDelegateProtocol import platform.UIKit.UIDocumentPickerViewController +import platform.UIKit.UIViewController import platform.UniformTypeIdentifiers.UTType import platform.UniformTypeIdentifiers.UTTypeApplication import platform.UniformTypeIdentifiers.UTTypeAudio @@ -57,6 +59,7 @@ private fun rememberDocumentPickerLauncher( onResult: (List) -> Unit, ): FilePickerLauncher { val scope = rememberCoroutineScope() + val currentUIViewController = LocalUIViewController.current val delegate = remember { @@ -114,7 +117,7 @@ private fun rememberDocumentPickerLauncher( } } - return remember { + return remember(currentUIViewController) { FilePickerLauncher( type = type, selectionMode = selectionMode, @@ -126,7 +129,7 @@ private fun rememberDocumentPickerLauncher( selectionMode = selectionMode, ) - UIApplication.sharedApplication.keyWindow?.rootViewController?.presentViewController( + currentUIViewController.presentViewController( pickerController, true, null, @@ -143,6 +146,7 @@ private fun rememberImageVideoPickerLauncher( onResult: (List) -> Unit, ): FilePickerLauncher { val scope = rememberCoroutineScope() + val currentUIViewController = LocalUIViewController.current val pickerDelegate = remember { object : NSObject(), PHPickerViewControllerDelegateProtocol { @@ -172,7 +176,7 @@ private fun rememberImageVideoPickerLauncher( } } - return remember { + return remember(currentUIViewController) { FilePickerLauncher( type = type, selectionMode = selectionMode, @@ -184,7 +188,7 @@ private fun rememberImageVideoPickerLauncher( selectionMode = selectionMode, ) - UIApplication.sharedApplication.keyWindow?.rootViewController?.presentViewController( + currentUIViewController.presentViewController( imagePicker, true, null, @@ -256,6 +260,7 @@ private fun createUIDocumentPickerViewController( return pickerController } + private fun createPHPickerViewController( delegate: PHPickerViewControllerDelegateProtocol, type: FilePickerFileType, @@ -288,6 +293,7 @@ private fun createPHPickerViewController( return picker } + actual class FilePickerLauncher actual constructor( type: FilePickerFileType, selectionMode: FilePickerSelectionMode,