I'm trying to use UIDocumentPickerViewController from SwiftUI using UIViewControllerRepresentable. I've managed to show a file picker dialog, but it doesn't allow multiple file selection even when I set allowsMultipleSelection to true.
Is this a bug or am I doing something wrong?
Here's the code:
And here's the test view which triggers the picker.
Is this a bug or am I doing something wrong?
Here's the code:
Code Block import SwiftUI import UIKit import CoreServices struct DocumentPicker: UIViewControllerRepresentable { typealias UIViewControllerType = UIDocumentPickerViewController func makeUIViewController(context: Context) -> UIDocumentPickerViewController { let picker = UIDocumentPickerViewController(documentTypes: [kUTTypeImage as String], in: .open) picker.allowsMultipleSelection = true return picker } func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: Context) { } }
And here's the test view which triggers the picker.
Code Block import SwiftUI struct ContentView: View { @State var isPickerPresented = false var body: some View { Button("Show Picker") { isPickerPresented = true }.sheet(isPresented: $isPickerPresented) { DocumentPicker() } } }
Finally it worked on my iPad too after updating to iPadOS/Xcode beta 2.
Actually it didn't work on the first try, then I added one line inside updateUIViewController, and now it works!
The weird thing is it still works correctly even after I remove the line in updateUIViewController...
Anyway, thanks for your help @OOPer and @marlonjames.
Actually it didn't work on the first try, then I added one line inside updateUIViewController, and now it works!
Code Block language func makeUIViewController(context: Context) -> UIDocumentPickerViewController { let picker = UIDocumentPickerViewController(documentTypes: [kUTTypeImage as String], in: .open) picker.allowsMultipleSelection = true return picker } func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: Context) { uiViewController.allowsMultipleSelection = true }
The weird thing is it still works correctly even after I remove the line in updateUIViewController...
Anyway, thanks for your help @OOPer and @marlonjames.