Display PDF file in Swift 4

17,401

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

Share:
17,401

Related videos on Youtube

GTeckHan Goh
Author by

GTeckHan Goh

Updated on June 04, 2022

Comments

  • GTeckHan Goh
    GTeckHan Goh almost 2 years
    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?

    • Abhishek Jain
      Abhishek Jain about 6 years
      Where is your docController defined? I can't see it in the above code.
    • GTeckHan Goh
      GTeckHan Goh about 6 years
      Hi, i define at the top
    • geisterfurz007
      geisterfurz007 about 6 years
      Possible duplicate of Open PDF file using swift
  • GTeckHan Goh
    GTeckHan Goh about 6 years
    ...sorry i miss out to adding here..so sorry.. i got add the func documentInteractionControllerViewControllerForPreview actually
  • GTeckHan Goh
    GTeckHan Goh about 6 years
    cant assign value of type WKWebView to type UIWebView!
  • GTeckHan Goh
    GTeckHan Goh about 6 years
    i already doing this but the pdf still cant show..the pdf get from one URL from DB
  • Mobile Team iOS-RN
    Mobile Team iOS-RN about 6 years
    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) }
  • GTeckHan Goh
    GTeckHan Goh about 6 years
    i hit error Thread1 : EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0) at let urlRequest = URLRequest.init(url: pdfFilePath!)
  • Mobile Team iOS-RN
    Mobile Team iOS-RN about 6 years
    Actually it's working code in my project, All you need to do is just replace your PDF file path.
  • GTeckHan Goh
    GTeckHan Goh about 6 years
    dunnoe why i hit Thread1 : EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0)
  • Apparao Mulpuri
    Apparao Mulpuri about 6 years
    ok. Download the PDF file from the URL and pass the file url path to the DocumentInteractionController.
  • GTeckHan Goh
    GTeckHan Goh about 6 years
    But..i need to use URL to get the pdf file oh
  • Apparao Mulpuri
    Apparao Mulpuri about 6 years
    right. write the pdf content to a file in your app's Documents folder. Get the file URL from that file.
  • GTeckHan Goh
    GTeckHan Goh about 6 years
    ya..currently i get from DB the URL
  • Apparao Mulpuri
    Apparao Mulpuri about 6 years
    You have the PDF url but no PDF content. You should download the PDF Content from that URL (URL from DB) and write to the file and pass that file url to DocumentInteractionController.
  • GTeckHan Goh
    GTeckHan Goh about 6 years
    which mean..inside my program need have the pdf file ?
  • GTeckHan Goh
    GTeckHan Goh about 6 years
    ...if i download the pdf and upload to my project..after that i do if let fileURL = Bundle.main.path(forResource: "myPDF", ofType: "pdf")
  • Mobile Team iOS-RN
    Mobile Team iOS-RN about 6 years
    Can you share me your pdf url?
  • GTeckHan Goh
    GTeckHan Goh about 6 years
  • Apparao Mulpuri
    Apparao Mulpuri about 6 years
    ok. Check this out on how to download and save the file in iOS app bundle. stackoverflow.com/questions/5323427/…
  • Mobile Team iOS-RN
    Mobile Team iOS-RN about 6 years
    Using above code i am able to load your PDF, Make sure you enable app transport security key into your plist file.
  • GTeckHan Goh
    GTeckHan Goh about 6 years
    how to do that ? please guild me
  • GTeckHan Goh
    GTeckHan Goh about 6 years
    how to do that ? please guild me
  • GTeckHan Goh
    GTeckHan Goh about 6 years
    Is App Transport Security Setting -> Allow Arbitrary Loads ??
  • Mobile Team iOS-RN
    Mobile Team iOS-RN about 6 years
    Yes add this key value. <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>