Messing around with webviews, opening in safari… , building a custom viewer using PDFKit… If only I knew QuickLooks existed before any of that. This is the native and the best method so far that I’ve seen for actually viewing PDFs and many other files in iOS. And it takes only few lines of code.
How did I come around to using this? Well opening in Safari may seem like the easiest solution for reading PDF’s, but just so happened that I needed to open PDF’s that I’ve downloaded and stored in app memory, and would you look at that! Safari doesn’t support it, as its not by default a PDF reader, so it cannot open local PDF’s… only external as a link…
import QuickLook
private var pdfPreviewUrl: URL?
extension YourViewController: QLPreviewControllerDataSource {
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
guard let url = self.pdfPreviewUrl else {
fatalError()
}
return url as QLPreviewItem
}
}