Swift: UIDocumentInteractionController is not working?

Raju picture Raju · May 26, 2017 · Viewed 9.8k times · Source

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.

Answer

Abhishek Thapliyal picture Abhishek Thapliyal · May 26, 2017

Try Like This:

Declare : var interaction: UIDocumentInteractionController?

Then add

 interaction = UIDocumentInteractionController(url: URL(string: "<PDF FILE PATH>")!)
 interaction.delegate = self
 interaction.presentPreview(animated: true) // IF SHOW DIRECT

OR if need any suggestion popup

interaction.presentOpenInMenu(from: /*<SOURCE BUTTON FRAME>*/, in: self.view, animated: true)

Implement Delegate -> UIDocumentInteractionControllerDelegate

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

public func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) {
    interaction = nil
}