Showing a PDF in SwiftUI
Since iOS 11, Apple has provided PDFKit, a robust framework to display and manipulate PDF documents in your applications.
As you can imagine, PDFKit is based on UIKit. However, in this recipe, we'll see how easy it is to integrate it with SwiftUI.
Getting ready
Let's create a new SwiftUI app in Xcode called PDFReader
.
For this, we need a PDF document to present. We provide an example in the https://github.com/PacktPublishing/SwiftUI-Cookbook-2nd-Edition-/blob/main/Resources/Chapter15/recipe3/PDFBook.pdf repository but feel free to use the PDF document of your choice.
Copy the document into the project:
With the PDF document in the project, we are ready to implement our PDF viewer in SwiftUI.
How to do it…
PDFKit provides a class called PDFView
to render a PDF document.
Because the PDFView
class is a subclass of UIView
, it must be encapsulated...