Open PDF file using swift

前端 未结 11 1986
萌比男神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条回答
  • 2020-11-28 05:37

    You can also use Quick Look Framework from Apple.

    It is very flexible.

    You can show your PDF file with zoom feature.

    Also you have support for all other types (like png, jpg, docx, txt, rtf, xlsx, zip, mov, etc) of files and it is very easy to use.

    Please refer this answer if you want detail description of using QuickLook.framework

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

    SWIFT 4+

    If has to open file from local cache/Documentdiectory which has file path

    Method 1: using 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
     }
    

    Method 2: using 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
    
    0 讨论(0)
  • 2020-11-28 05:41

    A modernized version of Akila's answer, with the benefit that it is a drop in, ready to use UIViewController that you can integrate into your app.

    import UIKit
    import PDFKit
    
    final class PDFViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let pdfView = PDFView(frame: view.frame)
            
            title = "Your_title_here"
            
            if let url = Bundle.main.url(forResource: "document_name_here", withExtension: "pdf"),
                let pdfDocument = PDFDocument(url: url) {
                pdfView.displayMode = .singlePageContinuous
                pdfView.autoScales = true
                pdfView.displayDirection = .vertical
                pdfView.document = pdfDocument
                
                view.addSubview(pdfView)
            }
        }
    }
    
    • It creates the PDFView during viewDidLoad and sets it to use the view's frame
    • The URL for the PDF file is safely unwrapped from the bundle and then a PDFDocument is created, if possible
    • Some common settings are used. Adjust as needed
    • Finally, it adds the PDFView as a subview of the controller's view
    0 讨论(0)
  • 2020-11-28 05:42

    If you simply want to view a PDF file you can load it into a 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))
    

    If you'd like to achieve more, a good framework is PSPDFKit.

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

    Apple added PDFKit framework in iOS 11

    Add a UIView to your view controller and make it's class to 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
                }
            }
        }
    }
    

    There are 4 display modes : singlePage, singlePageContinuous, twoUp, twoUpContinuous .

    0 讨论(0)
提交回复
热议问题