I am a relatively new iOS developer, so take all of this with a grain of salt.
The following worked for me:
- set allowsPickingMultipleItems to true
- create a ViewController that can take an input of
URL
, and another that can take an input of [URL]
. These ViewControllers must then show the document(s) associated with the URL(s) on screen.
- a single ViewController that can handle one or multiple documents would also work.
- in
documentBrowser(_:, didPickDocumentURLs:)
, check how many URL
s were passed in, and present one of the above ViewControllers (as appropriate)
example:
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)
}
}
To answer your additional questions:
- I'm not sure how it is recommended to implement this functionality
- I think opening one, then another document may be more suited to a UIDocumentPickerViewController
- I am not aware of any apps that implement this multiple-document experience. I do know, however, from trial and error, that the document browser looks just like it usually does, but with a "select" button in the top right. After pressing this, users can select documents and folders to open, or "select all."
Some caveats:
- If a folder is chosen, and the folder is not in the app's own directory, the app will not be granted access to documents inside the folder.
Note:
documentBrowser(_:, didPickDocumentURLs:)
will be renamed documentBrowser(_: didPickDocumentsAt:)
in iOS 12