Skip to content

iOS: File picker cannot be opened in another sheet #164

Open
@davidsoukup

Description

I'm trying to implement a file picker for iOS.

I have implemented the file picker function in the View model:

open class SelectFileViewModel: BaseViewModel<SelectFileContract.Event, SelectFileContract.State, SelectFileContract.Effect>() {

    override suspend fun handleEvent(event: SelectFileContract.Event) {
        when (event) {
            is SelectFileContract.Event.SelectDocument -> selectDocument(event.document)
        }
    }
    
        override fun createInitialState(): PepFormContract.State = PepFormContract.State.initialState
    
    private suspend fun selectDocument(document: Document) {
        val files = FileKit.pickFile(
            type = PickerType.File(
                extensions = listOf("pdf", "png", "jpg", "jpeg", "heic")
            ),
            title = null,
            mode = PickerMode.Multiple(5),
        )

        files?.let {
            setState { copy(requiredDocuments = requiredDocuments + (document to files)) }
        }
    }
}

Then, I have this component in iOS.

PepFormFileUpload( onSelect: { viewModel.setEvent(event: PepFormContractEventSelectDocument(document: document)) }, requiredDocument: requiredDocument, selectedFiles: viewModel.state.requiredDocuments[requiredDocument] as? [Filekit_corePlatformFile] )

Selecting files works, but when calling SelectDocument in another sheet, this error occurs:

Attempt to present <UIDocumentPickerViewController: 0x10192ce00> on <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10202aa00> (from <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10202aa00>) which is already presenting <_TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView_: 0x10985a800>.

iOS 18.1 & Xcode 16.0

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions