使用swift打开PDF文件

如何为应用程序添加PDF文件 ,单击按钮查看文件?完成后,您将返回到您所在的屏幕?

如果您只想查看PDF文件,可以将其加载到UIWebView中。

let url : NSURL! = NSURL(string: "http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf") webView.loadRequest(NSURLRequest(URL: url)) 

Swift 4.1:

 let url: URL! = URL(string: "http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf") webView.loadRequest(URLRequest(url: url)) 

如果你想获得更多,一个好的框架是PSPDFKit 。

适用于Xcode 8.1和Swift 3.0

将PDF文件保存在xcode的任何文件夹中。 假设文件名是’Filename.pdf’

  if let pdf = Bundle.main.url(forResource: "Filename", withExtension: "pdf", subdirectory: nil, localization: nil) { let req = NSURLRequest(url: pdf) yourWebViewOutletName.loadRequest(req as URLRequest) } 

如果要打开任何html文件,则同样适用。

您可以使用UIDocumentInteractionController在IOS中预览文件。 在视图控制器的文件中,添加UIDocumentInteractionController类型的属性

并实现它的简单委托方法

 self.documentInteractionController = UIDocumentInteractionController.init(URL: url) self.documentInteractionController?.delegate = self self.documentInteractionController?.presentPreviewAnimated(t‌​rue) func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController { return self } 

不要忘记在视图控制器的类中添加UIDocumentInteractionControllerDelegate

SWIFT 4+

如果必须从具有文件路径的本地缓存/ Documentdiectory打开文件

方法1:使用UIDocumentInteractionController

 class ViewController: UIViewController,UIDocumentInteractionControllerDelegate { //let path = Bundle.main.path(forResource: "Guide", ofType: ".pdf")! let dc = UIDocumentInteractionController(url: URL(fileURLWithPath: path)) dc.delegate = self dc.presentPreview(animated: true) } //MARK: UIDocumentInteractionController delegates func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { return self//or use return self.navigationController for fetching app navigation bar colour } 

方法2:使用WebView

  let webview = WKWebView(frame: UIScreen.main.bounds) view.addSubview(webview) webview.navigationDelegate = self webview.load(URLRequest(url: URL(fileURLWithPath: path)))//URL(string: "http://") for web URL 

Apple在iOS 11添加了PDFKit框架

将UIView添加到视图控制器并使其成为PDFView的类

在此处输入图像描述

 import UIKit import PDFKit class ViewController: UIViewController { @IBOutlet var pdfView: PDFView! override func viewDidLoad() { super.viewDidLoad() if let path = Bundle.main.path(forResource: "sample", ofType: "pdf") { if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) { pdfView.displayMode = .singlePageContinuous pdfView.autoScales = true pdfView.displayDirection = .vertical pdfView.document = pdfDocument } } } } 

有4种显示模式: singlePagesinglePageContinuoustwoUptwoUpContinuous

您也可以使用Apple的Quick Look Framework。

它非常灵活。

您可以使用缩放function显示PDF文件。

此外,您还支持文件的所有其他类型(如png,jpg,docx,txt,rtf,xlsx,zip,mov等),并且它非常易于使用。

请参考 如果您想要使用QuickLook.framework的详细说明,请回答此问题

您可以在Swift 4中使用此代码

  1. 导入PDFKit
  2. 复制此代码

     let pdfView = PDFView() pdfView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(pdfView) pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true guard let path = Bundle.main.url(forResource: "test", withExtension: "pdf") else { return } if let document = PDFDocument(url: path) { pdfView.document = document } 
 let openLink = NSURL(string : self.OtherContactorProfileVview.Result.CertificateList[index].CertificateFileLink) if #available(iOS 9.0, *) { let svc = SFSafariViewController(url: openLink! as URL) present(svc, animated: true, completion: nil) } else { let port : PDFViewer = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PDFViewer") as! PDFViewer port.strUrl = self.OtherContactorProfileVview.Result.CertificateList[index].CertificateFileLink self.navigationController?.pushViewController(port, animated: true) }