This summer, at their Worldwide Developers Conference (WWDC) 2017, Apple finally introduced PDFKit support for iOS. PDFKit has been in the AppKit for macOS since OS X 10.4, but not in the UIKit for iOS. Although Apple already used the PDFKit in iBooks and Mail previously, the framework wasn’t open to developers.
It brings in tons of features, such as opening, modifying, drawing and saving PDFs, including selecting and searching text. It also has improved accessibility support.
The main classes that most of you will need are the following:
- Used to display PDFs, including selecting content, navigating, zooming etc.
- Represents the PDF data or file allowing you to write, search and select PDF data.
- Render PDF data, add annotations, getting the page text and more.
- Additional content in PDFs including notes, links, forms etc.
To just show the PDF inside your application, you’ll only need to use PDFView and PDFDocument, which is very simple to do.
Simple showing of PDFs
And that’s it! To simply show a PDF from the web or locally, we initiate the PDFDocument with the specified URL and pass it to the PDFView to show it.
Modifying the PDFs
There are a number of methods that allow you to modify the PDF pages, including:
- removing a page
- inserting a page
- reordering the pages
Saving/sharing the loaded PDF
To save or share the loaded PDF you simply need the data of it – then you can use the standard UIActivityViewController. Luckily for us, Apple implemented the right method for us:
Combined with UIActivityViewController we can save or share our loaded PDF:
PDFKit is a powerful framework that was definitely missing in iOS. It includes many more features than just simple showing and it has definitely made the handling of PDFs much cleaner and easier.