2
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?

d4Rk
  • 6,622
  • 5
  • 46
  • 60
GTeckHan Goh
  • 97
  • 1
  • 3
  • 12

4 Answers4

6

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

  • ...sorry i miss out to adding here..so sorry.. i got add the func documentInteractionControllerViewControllerForPreview actually – GTeckHan Goh Feb 02 '18 at 07:11
  • cant assign value of type WKWebView to type UIWebView! – GTeckHan Goh Feb 02 '18 at 07:17
  • Yes you can do that by using this code. override func viewDidLoad() { super.viewDidLoad() let pdfFilePath = Bundle.main.url(forResource: "iostutorial", withExtension: "pdf") let urlRequest = URLRequest.init(url: pdfFilePath!) let webView = UIWebView(frame: self.view.frame) webView.loadRequest(urlRequest) self.view.addSubview(webView) } –  Feb 02 '18 at 07:22
  • i hit error Thread1 : EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0) at let urlRequest = URLRequest.init(url: pdfFilePath!) – GTeckHan Goh Feb 02 '18 at 07:32
  • Actually it's working code in my project, All you need to do is just replace your PDF file path. –  Feb 02 '18 at 07:40
  • dunnoe why i hit Thread1 : EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0) – GTeckHan Goh Feb 02 '18 at 07:46
  • Can you share me your pdf url? –  Feb 02 '18 at 09:09
  • http://www.finexuscards.com/wp-content/themes/finexuscards/pdf/FINEXUS-Cards-Privacy-Statement.pdf – GTeckHan Goh Feb 02 '18 at 09:23
  • Using above code i am able to load your PDF, Make sure you enable app transport security key into your plist file. –  Feb 02 '18 at 09:46
  • how to do that ? please guild me – GTeckHan Goh Feb 02 '18 at 09:49
  • Is App Transport Security Setting -> Allow Arbitrary Loads ?? – GTeckHan Goh Feb 02 '18 at 09:56
  • Yes add this key value. NSAppTransportSecurity NSAllowsArbitraryLoads –  Feb 02 '18 at 10:07
0

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.

0

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/

kd02
  • 422
  • 5
  • 14
0

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.

Diego Jiménez
  • 1,398
  • 1
  • 15
  • 26