Swift:UIDocumentInteractionController 不起作用?

Raj*_*aju 5 ios uidocumentinteraction swift

UIDocumentInteractionController 不适用于具有多个页面的大型 pdf 文件。

在我的代码中,

      var docController:UIDocumentInteractionController!
Run Code Online (Sandbox Code Playgroud)

...

    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)
    })
Run Code Online (Sandbox Code Playgroud)

和委托方法,

func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
    return self
}
Run Code Online (Sandbox Code Playgroud)

这是控制台中的警告,

2017-05-26 12:46:51.178894 MyApp [3350:1136818] [默认]查看服务确实终止并出现错误:错误域=_UIViewServiceInterfaceErrorDomain代码= 3“(null)”UserInfo = {Message =服务连接中断}#Remote

下面附上空白图片,

在此输入图像描述

请帮帮我谢谢。

Mad*_*ddy 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
    }
Run Code Online (Sandbox Code Playgroud)