Open PDF file using swift

前端 未结 11 1985
萌比男神i
萌比男神i 2020-11-28 04:50

How can I add a PDF file for an app , where you click on a button to view the file & when you\'re done you get back to screen you were at?

相关标签:
11条回答
  • you can use UIDocumentInteractionController to preview file in IOS. In the view controller's file, add a property of type UIDocumentInteractionController

    and implement a simple delegate method of it

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

    don't forget to add UIDocumentInteractionControllerDelegate in view controller's class

    0 讨论(0)
  • 2020-11-28 05:19

    You can use this code in Swift 4

    1. Import PDFKit
    2. Copy this code

      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
      }
      
    0 讨论(0)
  • 2020-11-28 05:19

    SWIFT 5

    An update to open the file from the Document directory (device) and present preview:

    let urlFile = URL(string: pathToFile)
    var documentInteractionController: UIDocumentInteractionController!    
    self.documentInteractionController = UIDocumentInteractionController.init(url: urlFile!)
    self.documentInteractionController?.delegate = self
    self.documentInteractionController?.presentPreview(animated: true)
    

    And UIDocumentInteractionControllerDelegate:

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

    If you want to dismiss the document preview you can use:

    self.documentInteractionController?.dismissPreview(animated: true)
    
    0 讨论(0)
  • 2020-11-28 05:20

    For Xcode 8.1 and Swift 3.0

    Save the PDF file in any folder of your xcode. Suppose the file name is '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)        
      }
    

    Same will apply if you want to open any html file.

    0 讨论(0)
  • 2020-11-28 05:20

    Check out PDFKit from IOS11

    Here is an example of a view controller which implements PDFView from PDFKit.

    import UIKit
    import PDFKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Add PDFView to view controller.
            let pdfView = PDFView(frame: self.view.bounds)
            self.view.addSubview(pdfView)
    
            // Fit content in PDFView.
            pdfView.autoScales = true
    
            // Load Sample.pdf file.
            let fileURL = Bundle.main.url(forResource: "Sample", withExtension: "pdf")
            pdfView.document = PDFDocument(url: fileURL!)
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 05:30
    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)
    
            }
    
    0 讨论(0)
提交回复
热议问题