Apple 的文档很少涉及基于 UIDocumentBrowserViewController
的应用程序,这些应用程序希望支持同时打开多个文档。
我想启用它,以便用户可以轻松地在两个或多个文档之间复制/粘贴,而不必退出回到文档浏览器,这在 iOS 上不是流畅的体验。
除了对 allowsPickingMultipleItems
的简短描述属性,我找不到任何东西。
对于单个文档 View ,Apple 推荐模态视图,但没有说明其他任何内容。
问题
- 实现多个打开文档的体验和 UI 的推荐方法是什么(如果有)?
- 有没有办法让用户打开一组文档,然后打开另一个文档,同时保持现有文档打开?
- 是否有实现此类体验的应用?
最佳答案
我是一个相对较新的 iOS 开发者,所以对所有这些持保留态度。
以下对我有用:
- 将 allowsPickingMultipleItems 设置为 true
- 创建一个可以接受
URL
输入的 ViewController,以及另一个可以接受[URL]
输入的 ViewController。然后,这些 ViewController 必须在屏幕上显示与 URL 关联的文档。- 可以处理一个或多个文档的单个 ViewController 也可以。
- 在
documentBrowser(_:, didPickDocumentURLs:)
中,检查传入了多少URL
,并呈现上述 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:)
in iOS 12
关于ios - 在基于 UIDocumentBrowserViewController 的应用程序中打开多个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50954300/