Swift: UIDocumentInteractionController is not working?

Viewed 11719

UIDocumentInteractionController is not working with large pdf files with multiple pages.

Here in my code,

      var docController:UIDocumentInteractionController!

...

    DispatchQueue.main.async (execute: { [weak self] in

            self?.docController = UIDocumentInteractionController(url: pdfFileURL!)
            self?.docController.delegate = self
            self?.docController.name = pdfFileURL?.lastPathComponent
            self?.docController.presentPreview(animated: true)
    })

and delegate method,

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

This is the warning in console,

2017-05-26 12:46:51.178894 MyApp [3350:1136818] [default] View service did terminate with error: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)" UserInfo={Message=Service Connection Interrupted} #Remote

Below attached is the blank image,

enter image description here

Please help me out thanks.

6 Answers

Be sure that your file is actually pdf file by checking its extension or if url path contains ".pdf"

Otherwise it will recognize it as text file and will not open it.

I just implemented UIDocumentInteractionController last week and it's working fine.

Do one thing declare as UIDocumentInteractionController as var below to class

then do allocation and initialization in viewDidLoad()as I did

documentInteractionController = UIDocumentInteractionController.init()
documentInteractionController?.delegate = self

and at the time of presenting I make it like this

documentInteractionController?.url = url
documentInteractionController?.presentPreview(animated: true)

Hope it will resolve your problem.

If you're loading a file from Bundle, this is a known bug in iOS 11.2.x. A workaround is copying the file to tmp:

let docUrl = Bundle.main.url(forResource: "Doc", withExtension: "pdf")!

// copy to tmp, because there is a bug in iOS 11.2.x,
// which will make pdf in UIDocumentInteractionController blank
let temporaryDirectoryPath = NSTemporaryDirectory()
let tmpDir = URL(fileURLWithPath: temporaryDirectoryPath, isDirectory: true)
let tmpUrl = tmpDir.appendingPathComponent("Doc.pdf")
do {
    if FileManager.default.fileExists(atPath: tmpUrl.path) {
        try FileManager.default.removeItem(at: tmpUrl)
    }
    try FileManager.default.copyItem(at: docUrl, to: tmpUrl)
    let reader = UIDocumentInteractionController(url: tmpUrl)
    reader.delegate = self
    reader.presentPreview(animated: true)
} catch let error {
    print("Cannot copy item at \(docUrl) to \(tmpUrl): \(error)")
}

Reference: https://forums.developer.apple.com/thread/91835

It does not issue with UIDocumentInteractionController. In my case the PDF file is corrupted, that's why it is showing me the same screen. Try to upload/open verified pdf file and you can preview same with UIDocumentInteractionController

Related