2

In short: I am currently working on an app with which you can scan a QR code. The QR code contains a path to a folder in Apple's own "Files" app. And in this folder I would like to view the PDF files and open them. My problem: I want to access a PDF file in a folder just to open the PDF file. When opening the UIDocumentPickerViewController, I want to open the target folder directly in which the file is stored. With the directoryURL property I try to enter the target URL directly so that it is taken over by the DocumentPickerViewController, but this only opens the last directory. If I change the initializer of the Document PickerView to "for Opening ContentTypes: [.folder]", then it jumps to the right folder, but I can't see any PDF files because it only shows me the folder files. BUT! : When I test my code via the simulator, my function works. And when I run the App on my iPhone, the DomumentPickerView keeps jumping to the last directory. According to the Apple documentation about the property: "directoryURL" it says: "Set this property to specify the starting directory for the document picker. This property defaults to nil. If you specify a value, the document picker tries to start at the specified directory. Otherwise, it starts with the last directory chosen by the user.” But why does it show me the right folder when I search for content type [.folder] and when I search for content type [.pdf] only the last directory.

HEEEELP

struct DocumentPicker : UIViewControllerRepresentable {

@Binding var folderURL : URL?
@Binding var documentURL : URL?


func makeUIViewController(context: Context) -> UIDocumentPickerViewController {

let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.pdf], asCopy: true)

documentPicker.delegate = context.coordinator

if let url = folderURL {
documentPicker.directoryURL = url
}

return documentPicker
}

func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: Context) {
print("Show DocumentPickerViewController")
}

}
Denis
  • 31
  • 1
  • 3

1 Answers1

1

Don’t use type .folder ; this is to get access to a whole folder and is not implemented by many file providers. Use the actual type of the file you want to open (pdf) and populate the starting directory with the URL of the directory, not the URL of the PDF file. Also keep in mind that some file providers populate their tree lazily, so the folder is not guaranteed to exist the first time you ask for it. The actual file paths may also be device specific (some file providers use UUIDs) so sharing an URL with a QR code across devices may not work.

Thomas Deniau
  • 2,488
  • 1
  • 15
  • 15
  • 1
    i didn't use the type: .folder in my code. I just wanted to say that it works so that the DocumentPicker jumps directly to the right path. In my code I use the type: .pdf and pass the following path to the "documentPicker.directoryURL" property: "file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/FindMyFile%20Demo%20Folder/FolderOne/MyFolder/" However, when I then open my DocumentPicker, only the path that was opened last opens and not the one that I passed to the "directoryURL". – Denis Aug 16 '22 at 12:53
  • Interesting. Seems worth filing a bug to Apple. – Thomas Deniau Aug 17 '22 at 14:29