Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift 4中显示PDF文件_Ios_Swift_Pdf_Webview - Fatal编程技术网

Ios 在Swift 4中显示PDF文件

Ios 在Swift 4中显示PDF文件,ios,swift,pdf,webview,Ios,Swift,Pdf,Webview,上面的代码我无法显示pdf文件。有人能帮忙吗?通过查看代码,您似乎错过了添加UIDocumentInteractionControllerDelegate委托方法 import UIKit import WebKit class ViewController: UIViewController, UIDocumentInteractionControllerDelegate { var docController: UIDocumentInteractionController!

上面的代码我无法显示pdf文件。有人能帮忙吗?

通过查看代码,您似乎错过了添加UIDocumentInteractionControllerDelegate委托方法

import UIKit
import WebKit

class ViewController: UIViewController, UIDocumentInteractionControllerDelegate {

    var docController: UIDocumentInteractionController!

    override func viewDidLoad() {
        super.viewDidLoad()

        docController = UIDocumentInteractionController.init(url: URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(urlVal!))
        docController.delegate = self
        docController.presentPreview(animated: true)       
    }


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

您还可以通过将PDF加载到WKWebView来查看它

    class ViewController: UIViewController,UIDocumentInteractionControllerDelegate {
        override func viewDidLoad() {
            super.viewDidLoad()

           var docController = UIDocumentInteractionController.init(url: URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(urlVal!))
            docController.delegate = self
            docController.presentPreview(animated: true)
        }
        func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
            return self
        }
    }

基本上,您缺少UIDocumentInteractionControllerDelegate实现。对于预览,您应该实现此方法

    override func viewDidLoad() {
        super.viewDidLoad()

        let pdfFilePath = Bundle.main.url(forResource: "iostutorial", withExtension: "pdf")
        let urlRequest = URLRequest.init(url: pdfFilePath!)
        webView = WKWebView(frame: self.view.frame)
        webView.load(request)
        self.view.addSubview(webView)

    }
返回显示预览所需的ViewController。如果传递自视图控制器,它将在现有视图控制器中显示PDF预览。如果要在同一控制器中显示预览,只需在视图控制器中执行此操作

    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController

如果您已经在代码中执行此操作,则PDF URL路径很可能出错。

我将使用Quicklook框架,它支持多种文档类型:

  • 我的工作文件
  • Microsoft Office文档
  • PDF文件
  • 图像
  • 文本文件
  • 富文本格式文档
  • 逗号分隔值文件(csv)
支持相关文档的共享,易于实现

按照本教程介绍如何在Swift中执行此操作:

Swift 5

导入此框架

    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
        return self
    }
然后在你需要的时候叫这个句子

import SafariServices

代码将显示带有pdf的Safari视图。

您的docController在哪里定义?我在上面的代码中看不到它。嗨,我在最上面定义了一个可能的副本…抱歉,我错过了在这里添加…非常抱歉。。我得到了添加func DocumentInteractionControllerServiceControllerForpReview的命令,实际上可以将WKWebView类型的值赋给UIWebView类型!是的,您可以使用此代码来完成此操作。重写func viewDidLoad(){super.viewDidLoad()让PdfielPath=Bundle.main.url(forResource:“iostutorial”,扩展名:“pdf”)让urlRequest=urlRequest.init(url:PdfielPath!)让webView=UIWebView(frame:self.view.frame)webView.loadRequest(urlRequest)self.view.addSubview(webView)}我在let urlRequest=urlRequest.init(url:pdfFilePath!)中遇到了错误Thread1:EXC_BAD_指令(code=EXC_1386_INVOP,subcode=0x0),实际上它在我的项目中工作,你所需要做的就是替换你的PDF文件路径。我已经在做了,但是PDF仍然无法显示。PDF从DBok的一个URL获取。从URL下载PDF文件,并将文件URL路径传递给DocumentInteractionController。但是..我需要使用URL来获得正确的PDF文件。将pdf内容写入应用程序文档文件夹中的文件。从该文件获取文件URL。ya..目前我从DB获取URL
if let url = URL(string: "YOUR_PDF_URL") {
  let config = SFSafariViewController.Configuration()
  config.entersReaderIfAvailable = true

  let vc = SFSafariViewController(url: url, configuration: config)
  present(vc, animated: true)
}