Home » Swift » UIDocumentMenuViewController dismisses presenting view controller

UIDocumentMenuViewController dismisses presenting view controller

Posted by: admin November 30, 2017 Leave a comment

Questions:

I have a VC “A” that present VC “B” modally. B presents a UIDocumentMenuViewController
The UIDocumentMenuDelegate protocol is implemented in B.

As soon as documentMenuWasCancelled(_ documentMenu:) or documentMenu(_:, didPickDocumentPicker:) gets called the dismiss(animated:,completion:) of B gets called and i have no clue why.

Here’s my code

func presentDocumentPicker() {
    let documentTypes = [
        kUTTypeCompositeContent as String,
        kUTTypePDF as String,
        "com.microsoft.word.doc",
        "vnd.openxmlformats-officedocument.wordprocessingml.document"
    ]
    let documentMenuViewController = UIDocumentMenuViewController(
        documentTypes: documentTypes,
        in: .import
    )
    documentMenuViewController.delegate = self
    present(
        documentMenuViewController,
        animated: true,
        completion: nil
    )
}

func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
    print("did pick")
}

func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
    print("was cancelled")
}

As you can see I do nothing in the implemented delegate function. And still B gets dismissed. I don’t get it.

Answers:

This is caused by documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) method which is called when you click on an action or when you cancel UIDocumentMenuViewController.

I posted a solution here: https://stackoverflow.com/a/45505488/6381503

Hope it helps.