Search code examples
swiftswiftuiuidocumentpickerviewcontroller

FileImporter / UIDocumentPickerViewController not showing contents


Both the .fileImporter function (in SwiftUI) and the UIDocumentPickerViewController are shown as intended, but the do not show any contents. Instead it says: "Content not available" on the Shared and Recent Icons and "My iPhone is empty" on the Browse Icon.

How did I implement them? .fileImporter:

var body: some View {
    VStack {
        // ...
    }.padding(.horizontal, 25)
        .fileImporter(isPresented: $selectingFile, allowedContentTypes: [.item], onCompletion: { _ in })
}

UIDocumentPickerViewController:

struct ImportDocumentView: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> some UIViewController {
        let controller = UIDocumentPickerViewController(forOpeningContentTypes: [.item])
        controller.allowsMultipleSelection = false
        return controller
    }

    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
}

What I thought could be the problems:

  • Wrong UTType -> tried a load of different options there; none helped.
  • Emulator Problem -> I don't have an iPhone to test this. I need to make it work in the emulator. Other people had problems with the emulator, but not showing any data was not one of them.
  • Access Permissions -> In my understanding I should at least have access to the app specific folder, but even that is not showing. I also tried to set "NSFileProviderDomainAccess" in Info.plist, but that didn't work either. (I might have done something wrong in the process though)

Solution

  • I found the answer to my own question: I had to "import UniformTypeIdentifiers"

    Xcode doesn't complain about not having it, it just doesn't show any files or folders. Only indicator was a different color for the element in the allowedContentTypes: [] collection.

    Edit: For those still not seeing much: the Xcode iPhone Simulator doesn't have any example/inital files or folders in there. I'd recommend to create a folder with the files app on the phone to see some feedback. If you want files in there use Swift's fileExporter.