Swift: UIDocumentInteractionController is not working?

两盒软妹~` 提交于 2019-12-11 00:43:45

问题


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,

Please help me out thanks.


回答1:


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
}



回答2:


(1) This issue usually occur in simulator. Check it on real device.

If that not the case

(2) Try this,

class ViewController: UIViewController, UIDocumentInteractionControllerDelegate {

    var docController:UIDocumentInteractionController!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        if let fileURL = NSBundle.mainBundle().pathForResource("SamplePDF1", ofType: "pdf") { // Use if let to unwrap to fileURL variable if file exists
            docController = UIDocumentInteractionController(URL: NSURL(fileURLWithPath: fileURL))

            docController.name = NSURL(fileURLWithPath: fileURL).lastPathComponent

            docController.delegate = self

            docController.presentPreviewAnimated(true)
        }


    }

    func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
        return self
    }
     func documentInteractionControllerDidEndPreview(controller: UIDocumentInteractionController) {
        docController = nil
    }



回答3:


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.




回答4:


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.




回答5:


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




回答6:


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



来源:https://stackoverflow.com/questions/44196066/swift-uidocumentinteractioncontroller-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!