首页 > 解决方案 > 使用 UIDocumentBrowserViewController 选择文件夹

问题描述

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

我尝试将 allowedContentTypes 设置为文件夹的 UTI,“public.directory”,但文档浏览器不允许选择文件夹。

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

标签: iosswiftuikituidocumentbrowserviewcontroller

解决方案


这是适用于 iOS 13的官方 Apple 解决方案:

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

常数kUTTypeFolder来自import CoreServices


推荐阅读