iosswiftuikitios11uidocumentinteraction

UIDocumentInteractionController does not open other app in iOS 11


My apps ability to share a PDF to another third party app has broken in iOS 11. It shows the share sheet but when I choose to open it in a different (third-party) app, it simply dismisses the share sheet and does nothing.

Things I've confirmed:

Has anyone else experienced this or have any ideas on how to fix it?

Here is the full code of an example app that shows the problem. I created it in a new "single view" app. The storyboard only has two buttons hooked up to the two actions.

class ViewController: UIViewController {
    var docController: UIDocumentInteractionController!

    @IBAction func open(sender: UIButton) {
//        self.docController.presentPreview(animated: true)
        self.docController.presentOptionsMenu(from: sender.frame, in:self.view, animated:true)
    }

    @IBAction func check() {
        print(self.docController)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = Bundle.main.url(forResource: "OrderForm", withExtension: "pdf")!
        self.docController = UIDocumentInteractionController(url: url)
        self.docController.delegate = self
    }
}

extension ViewController: UIDocumentInteractionControllerDelegate {
    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
        return self
    }

    func documentInteractionController(_ controller: UIDocumentInteractionController, willBeginSendingToApplication application: String?) {
        print("will begin")
    }

    func documentInteractionController(_ controller: UIDocumentInteractionController, didEndSendingToApplication application: String?) {
        print("did end")
    }
}

I also filed a bug with Apple (Problem ID: 34772214)


Solution

  • The problem was that I was trying to share a URL to a file inside the app bundle. It seems Apple's apps have permission to access that URL but third-party apps don't. I solved this by first copying the resource to the cache directory and then using that cache directory URL for sharing.