UIDocumentPickerViewController - jump to target folder with directoryURL

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.


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")
}

}
UIDocumentPickerViewController - jump to target folder with directoryURL
 
 
Q