Selecting a folder using UIDocumentPickerViewController

The following WWDC session saya that the user can select a folder and we can read all files in a folder

https://developer.apple.com/videos/play/wwdc2019/719/


But the delegte is never called when the user taps on a folder.
Does anybody know how the user can select a folder so that the app can get a callback with the URL of the selected folder.

Here is the code that I am using:


    let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypeFolder)],  in: .open)
    documentPicker.delegate = self
    documentPicker.allowsMultipleSelection = true
    present(documentPicker, animated: true) {
        print("done presenting")
    }

Replies

it's called when the user taps on "Done"


Here is sample code to list files from an external USB-C connected device


func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]){        
        print(urls)
        self.pickedFolderURL = urls.first!
}

@IBAction func readFiles(){
        // Reading the Content of a Picked Folder
        let shouldStopAccessing = pickedFolderURL.startAccessingSecurityScopedResource()
        defer {
            if shouldStopAccessing {
                pickedFolderURL.stopAccessingSecurityScopedResource()
            }
        }
        var coordinatedError:NSError?
        NSFileCoordinator().coordinate(readingItemAt: pickedFolderURL, error: &coordinatedError) { (folderURL) in
            let keys : [URLResourceKey] = [.nameKey, .isDirectoryKey]
            let fileList = FileManager.default.enumerator(at: pickedFolderURL, includingPropertiesForKeys: keys)!
            logString = ""
            for case let file as URL in fileList {
                
                let newFile = file.path.replacingOccurrences(of: pickedFolderURL.path, with: "")
                if(newFile.hasPrefix("/.") == false){ //exclude hidden
                    print(file)
                    logString += "\n\(file)"
                }
            }
            self.logTextView.text = logString
        }
    }