import UIKit
import PDFKit
import SVProgressHUD
class PdfViewController: UIViewController {
@IBOutlet weak var dummyPdfView: UIView!
@IBOutlet weak var courseLAbel: UILabel!
var pdfView : PDFView!
var pdfString = ""
override func viewDidLoad() {
super.viewDidLoad()
setPDF()
}
func setPDF() {
SVProgressHUD.show(withStatus: "Loading...")
DispatchQueue.global(qos: .background).async {
if let pdfURL = URL(string: self.pdfString), let pdfDocument = PDFDocument(url: pdfURL) {
DispatchQueue.main.async {
// Create and configure the PDFView on the main thread
let newPDFView = PDFView(frame: self.dummyPdfView.bounds)
newPDFView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.dummyPdfView.addSubview(newPDFView)
// Set properties and assign document to PDFView
newPDFView.autoScales = true
newPDFView.displayDirection = .vertical
newPDFView.displayMode = .singlePageContinuous
newPDFView.document = pdfDocument
SVProgressHUD.dismiss()
}
} else {
DispatchQueue.main.async {
SVProgressHUD.showError(withStatus: "Failed to load PDF")
}
}
}
}
@IBAction func backButtonPressed(_ sender: Any) {
SVProgressHUD.dismiss()
self.dismiss(animated: true)
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter