简体   繁体   中英

Opening multiple documents in a UIDocumentBrowserViewController based app

Apple's documentation is thin on the subject of UIDocumentBrowserViewController -based apps that want to support opening multiple documents simultaneously.

I'd like to enable this, so that the user can copy/paste between two or more documents easily, without having to exit back to the document browser, which is not a fluid experience on iOS.

Apart from a terse description of the allowsPickingMultipleItems property, I couldn't find anything.

For a single document view, Apple recommends a modal view, but doesn't say anything else.

Questions

  1. What (if any) is the recommended way to implement the experience and UI of multiple open documents?
  2. Is there a way for the user to open a set of documents, then open another document while keeping the existing documents open?
  3. Are there apps that implement such an experience?

I am a relatively new iOS developer, so take all of this with a grain of salt.

The following worked for me:

  1. set allowsPickingMultipleItems to true
  2. 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.
  3. 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:

  1. I'm not sure how it is recommended to implement this functionality
  2. I think opening one, then another document may be more suited to a UIDocumentPickerViewController
  3. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM