Contents

documentBrowser(_:didPickDocumentURLs:)

Tells the delegate that the user has selected one or more documents.

Declaration

optional func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL])

Parameters

  • controller:

    The current document browser.

  • documentURLs:

    An array of URLs for the selected documents.

    If the document browser’s Allowspickingmultipleitems property is True, the array contains one or more URLs. If False, it contains only a single URL.

Mentioned in

Discussion

Implement this method to process the documents selected by the user. Typically, you create a view controller to display the selected documents, then present that view modally, like in the following example.

// Did Select Documents
func documentBrowser(_ controller: UIDocumentBrowserViewController,
                     didPickDocumentURLs documentURLs: [URL]) {
    
    assert(controller.allowsPickingMultipleItems == false)
    
    assert(documentURLs.count > 0,
           "*** We received an empty array of documents ***")
    
    assert(documentURLs.count <= 1,
           "*** We received more than one document ***")
    
    guard let url = documentURLs.first else {
        fatalError("*** No URL Found! ***")
    }
    
    openDocument(controller, forFileURL: url)
}

private func openDocument(_ controller: UIDocumentBrowserViewController,
                          forFileURL url: URL) {
    
    let doc = // Create a UIDocument subclass for the selected URL.

    let editor = // Create a view controller to edit the document.

    // Optionally, set up a transition controller here...
        
    doc.open { (success) in
        guard success else {
            // Handle the error here...
        }
        
        // Present the document
        controller.present(editor, animated: true, completion: nil)
    }
}