GTeckHan Goh
GTeckHan Goh

Reputation: 97

Display PDF file in Swift 4

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
     }
}

Above code I'm not able to display the pdf file. Can anyone help?

Upvotes: 2

Views: 19174

Answers (4)

Diego Jiménez
Diego Jiménez

Reputation: 1526

Swift 5

Import this framework

import SafariServices

Then call this sentences whenever you need

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)
}

The code will present a Safari view with the pdf on it.

Upvotes: 0

kd02
kd02

Reputation: 430

I would use the Quicklook framework instead, it supports a wide range of document types:

  • iWork documents
  • Microsoft Office documents
  • PDF files
  • Images
  • Text files
  • Rich-Text Format documents
  • Comma-Separated Value files (csv)

Supports sharing of the relevant documents as well and is easy to implement.

Follow this tutorial on how to do it in Swift: https://www.appcoda.com/quick-look-framework/

Upvotes: 0

Apparao Mulpuri
Apparao Mulpuri

Reputation: 159

Basically you are missing the UIDocumentInteractionControllerDelegate implementation. For preview, you should implement this method

    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController

Return which ViewController need to display the Preview. If you pass the self View Controller it will display the PDF preview in the existing view controller modally. Just do this one in your View controller if you want to display the preview in the same controller.

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

If you are already doing this in your code, there is high chance of PDF URL path might be wrong.

Upvotes: 0

user9106403
user9106403

Reputation:

By seeing your code it seems that you missed to add UIDocumentInteractionControllerDelegate delegate method.

    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
        }
    }

OR

You can also view PDF by loading it into WKWebView.

    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)

    }

enter image description here

Upvotes: 6

Related Questions