Vitor Enes
Vitor Enes

Reputation: 283

SwiftUI: .fileImporter modifier not updating binding when dismissed by tapping "outside" the file picker

I am using the ".fileImporter" modifier in a SwiftUI view; it works great, except for one very annoying issue that I have not been able to find a solution for anywhere.

The "picker", which is bound to a state variable showFileImporter as required by the documentation (initially set to false), and configured to allow multiple selection, is presented by "toggling" the state variable when the user taps a button. Up to this point, all is good: the picker presents, I can effectively pick files (and get the corresponding URLs collection onCompletion), which in turn dismisses the picker view and sets the bound state variable showFileImporter back to false, as it should. Also, if after presenting the picker the user taps the Cancel button on the view UI, the picker is appropriately dismissed and the bound variable set to false- again, as it should.

The issue comes when you dismiss the picker by tapping outside the picker view.

The picker gets dismissed, alright, BUT the bound variable DOES NOT get updated - thus, in my case, the user has to tap the "pick files" button twice in order for the picker to preset itself, obviously because when dismissed in this way the value of the showFileImporter state variable is not updated and the toggle action on it triggered by tapping the button will set it to false "first", and then to true on the second tap, which then triggers the picker presentation.

Anyone has any idea if this is a bug? Am I doing something inherently wrong, or missing something? Just for completion, I am including a sample code that exhibits this behaviour:

struct ContentView: View {

    @State private var showFileImporter: Bool = false

    var body: some View {
        
        VStack {
            Button("Pick Some Files...") {
                showFileImporter.toggle()                
            }
        }
        .fileImporter(
            isPresented: $showFileImporter, 
            allowedContentTypes: [.video, .audio, .image], 
            allowsMultipleSelection: true, 
            onCompletion: { result in
                /* ...some code here */
        })
        
    }
}

Any pointers, hints or help at all is greatly appreciated, so thanks in advance.

Upvotes: 3

Views: 1028

Answers (1)

Vitor Enes
Vitor Enes

Reputation: 283

This is a certified bug (confirmed by Apple). Hopefully will be solved soon.

Upvotes: 3

Related Questions