In this blog, I have summarised the steps along with the code required to develop a basic PDF viewer iOS app in SwiftUI, using it a user can open and view PDF files. Sometime back, I had been working on a personal project which required PDF files to be loaded and editable ie adding annotations, color editing, etc. It was more of a PDF viewer and annotator app, basically.
I started developing the project, initially, in the Swift framework using the PDFKit package. But later, I just decided to try out the same in SwiftUI, instead. As I had been learning about SwiftUI and I found it to be really elegant, sleek, swift (aptly named), and fun to work on. Kind of felt it similar to Flutter’s framework, just a personal perspective.
For viewing a PDF file or document using PDFKit, only two steps are required for Swift, and in the case of SwiftUI framework, an additional step is needed 🙂
- Fetch a Document object from PDFDocument class by providing the URL/File path of your pdf file
- Assign this Document object to a PDFView class object
- You need to create a UIView generator wrapper for PDFView class, as SwiftUI doesn’t have a readily available UIView subclass for it.
Now you can directly use this PDFViewUI object in SwiftUI framework layout
And this is how the app looks on iPad and iPhone simulators.
If you need the source code for your reference, then you can find it here:
Hope you find this post helpful.