我是一个相对较新的 iOS 开发人员,所以对这一切持保留态度。
以下对我有用:
- 将 allowPickingMultipleItems 设置为 true
- 创建一个可以接受输入的 ViewController
URL
和另一个可以接受输入的 ViewController [URL]
。然后,这些 ViewController 必须在屏幕上显示与 URL 关联的文档。
- 可以处理一个或多个文档的单个 ViewController 也可以工作。
- in
documentBrowser(_:, didPickDocumentURLs:)
,检查传入了多少URL
s,并呈现上述 ViewController 之一(视情况而定)
例子:
class DocumentBrowserViewController: UIDocumentBrowserViewController, UIDocumentBrowserViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
allowsDocumentCreation = false
allowsPickingMultipleItems = true
// -snip-
}
// MARK: UIDocumentBrowserViewControllerDelegate
// -snip-
func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL]) {
if documentURLs.count < 1 {
return
} else if documentURLs.count == 1 {
presentDocument(at: documentURLs[0])
} else {
presentDocuments(at: documentURLs)
}
}
// -snip-
// MARK: Document Presentation
func presentDocument(at documentURL: URL) {
// present one document
// example:
// let vc = SingleDocumentViewController()
// vc.documentURL = documentURL
// present(vc, animated: true, completion: nil)
}
func presentDocuments(at documentURLs: [URL] {
// present multiple documents
// example:
// let vc = MultipleDocumentViewController()
// vc.documentURLs = documentURLs
// present(vc, animated: true, completion: nil)
}
}
要回答您的其他问题:
- 我不确定如何建议实现此功能
- 我认为打开一个,然后另一个文档可能更适合UIDocumentPickerViewController
- 我不知道有任何应用程序实现了这种多文档体验。但是,我确实知道,通过反复试验,文档浏览器看起来就像通常一样,但右上角有一个“选择”按钮。按下此按钮后,用户可以选择要打开的文档和文件夹,或“全选”。
一些警告:
- 如果选择了一个文件夹,并且该文件夹不在应用程序自己的目录中,则应用程序将无法访问该文件夹内的文档。
注意:
documentBrowser(_:, didPickDocumentURLs:)
将documentBrowser(_: didPickDocumentsAt:)
在 iOS 12中重命名