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 have provided a sample PDF document in the repository but feel free to use the PDF document of your choice:
Copy the PDF document into the project:
Figure 17.14: PDF document in the Xcode project
With the PDF document added as a resource to 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...