使用 UIDocumentBrowserViewController 选择文件夹

Selecting folders with UIDocumentBrowserViewController

我正在开发一个新的应用程序,我希望用户能够 select 来自 iCloud 或外部驱动器的文件夹。我如何允许他们使用 UIDocumentBrowserViewController select 文件夹?

我已尝试将 allowedContentTypes 设置为文件夹的 UTI,'public.directory',但文档浏览器不允许 selected 文件夹。

UIDocumentBrowserViewController(forOpeningFilesWithContentTypes: ["public.directory"])

这是 iOS 13 的 official Apple solution

let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeFolder as String], in: .open)
documentPicker.delegate = self
documentPicker.directoryURL = startingDirectory
present(documentPicker, animated: true)

常数kUTTypeFolder来自import CoreServices