PDF文件未在UIDocumentInteractionController中打开

时间:2017-02-22 07:00:21

标签: ios iphone swift

我在模拟器上工作,没有像adobe这样的应用程序打开pdf文件....我正在使用UIDocumentInteractionController打开pdf文件,如下所示。

let url=URL(fileURLWithPath: "/Users/sai/Library/Developer/CoreSimulator/Devices/425D8615-ADEC-4334-A639-C30B1E675EFA/data/Containers/Data/Application/AB3787BF-F6AC-4111-9847-A0FDC4E899F8/tmp/samplepdf.pdf")

    print("--------------------------------- str is \(url)")
    if (url != nil) {
        // Initialize Document Interaction Controller
        self.documentInteractionController = UIDocumentInteractionController(url: url)
        // Configure Document Interaction Controller
        self.documentInteractionController?.delegate = self
        // Preview PDF
        self.documentInteractionController?.presentOptionsMenu(from: self.view.frame, in: self.view, animated: true)
    }


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

我的屏幕显示如下 No pdf is being displayed here...is it because there is no app in my simulator which could open pdf files?

2 个答案:

答案 0 :(得分:0)

不确定任何模拟器应用程序是否会处理pdf文档 - 也许是新闻?但您可以将webView添加到视图控制器并使用它来查看您的pdf。

@IBOutlet weak var webView: UIWebView!

对您的网址进行常规安全检查,然后:

webView.loadRequest(NSURLRequest(url: url as URL) as URLRequest)

答案 1 :(得分:0)

我认为存在路径问题。

替换

let url=URL(fileURLWithPath: "/Users/sai/Library/Developer/CoreSimulator/Devices/425D8615-ADEC-4334-A639-C30B1E675EFA/data/Containers/Data/Application/AB3787BF-F6AC-4111-9847-A0FDC4E899F8/tmp/samplepdf.pdf")

let url = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("samplepdf.pdf")

适用于Swift 3 / Xcode 8

let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("samplepdf.pdf") as NSURL

希望这会有所帮助。