Swift:UIDocumentInteractionController不工作?

时间:2017-05-26 07:32:39

标签: ios swift uidocumentinteraction

UIDocumentInteractionController无法处理包含多个页面的大型pdf文件。

在我的代码中

      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)
    })

和委托方法,

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

这是控制台中的警告,

  

2017-05-26 12:46:51.178894 MyApp [3350:1136818] [默认]查看服务确实因错误而终止:错误Domain = _UIViewServiceInterfaceErrorDomain Code = 3“(null)”UserInfo = {Message = Service Connection Interrupted} #remote中

下面附有空白图片,

enter image description here

请帮助我谢谢。

6 个答案:

答案 0 :(得分:1)

尝试这样:

声明:public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { return self } public func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) { interaction = nil }

然后添加

npm install --save <package_name>

如果需要任何建议弹出窗口

npm install bootstrap@4.0.0-alpha.6

实施代表 - &gt; npm install bootstrap

4.0.0-alpha.6

答案 1 :(得分:0)

(1)此问题通常发生在模拟器中。在真实设备上查看。

如果不是这样的话

(2)试试这个,

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
    }

答案 2 :(得分:0)

通过检查文件的扩展名或网址路径是否包含“ .pdf”,确保文件实际上是pdf文件。

否则,它将识别为文本文件,并且不会将其打开。

答案 3 :(得分:0)

我上周刚刚实现了UIDocumentInteractionController,并且运行良好。

要做一件事,将下面的var声明为UIDocumentInteractionController

然后像我一样在viewDidLoad()中进行分配和初始化

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

在演示时,我做到了

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

希望它可以解决您的问题。

答案 4 :(得分:0)

如果您要从Bundle加载文件,则这是iOS 11.2.x中的已知错误。解决方法是将文件复制到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)")
}

参考:https://forums.developer.apple.com/thread/91835

答案 5 :(得分:0)

它与UIDocumentInteractionController无关。 就我而言,PDF文件已损坏,这就是为什么它向我显示相同的屏幕。 尝试上传/打开已验证的pdf文件,您可以使用UIDocumentInteractionController进行预览