Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open PDF file using swift

Tags:

ios

swift

iphone

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?

like image 801
user3706773 Avatar asked Nov 12 '14 09:11

user3706773


2 Answers

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.

like image 163
Jasper Blues Avatar answered Sep 26 '22 01:09

Jasper Blues


Apple added PDFKit framework in iOS 11

Add a UIView to your view controller and make it's class to PDFView

enter image description here

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 .

like image 32
Akila Wasala Avatar answered Sep 25 '22 01:09

Akila Wasala