Swift:UIDocumentInteractionController不工作?

问题描述 投票:2回答:5

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

请帮我谢谢。

ios swift uidocumentinteraction
5个回答
1
投票

尝试这样:

声明:var interaction: UIDocumentInteractionController?

然后加

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

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

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

实施代表 - > UIDocumentInteractionControllerDelegate

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

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

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
    }

0
投票

通过检查其扩展名或者url路径是否包含“.pdf”,确保您的文件实际上是pdf文件

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


0
投票

我上周刚刚实现了UIDocumentInteractionController,它工作正常。

做一件事就是将UIDocumentInteractionController声明为var下面的类

然后在viewDidLoad()as中进行分配和初始化

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

在呈现的时候,我就这样做了

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

希望它能解决你的问题。


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

© www.soinside.com 2019 - 2024. All rights reserved.