Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: UIDocumentInteractionController is not working?

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.

like image 250
Raju Avatar asked May 26 '17 07:05

Raju


3 Answers

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
}
like image 55
Abhishek Thapliyal Avatar answered Nov 04 '22 15:11

Abhishek Thapliyal


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.

like image 1
fatinho Avatar answered Nov 04 '22 15:11

fatinho


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.

like image 1
Mubin Mall Avatar answered Nov 04 '22 16:11

Mubin Mall