Blog Post

Adding a Document Viewer to a Mac Catalyst App

Illustration: Adding a Document Viewer to a Mac Catalyst App

Apple introduced Mac Catalyst in WWDC 2019 to allow developers to build cross-platform applications for iOS and macOS. Prior to Mac Catalyst, building these cross-platform applications required the knowledge of both the AppKit framework and UIKit. Now, with the introduction of Mac Catalyst, developers can build these apps using UIKit itself.

In this blog post, we’ll look at how to leverage the PSPDFKit for Catalyst SDK for adding a document viewer to a Mac Catalyst app.

Getting Started

This blog post assumes you already have an existing Xcode project with the Mac Catalyst target enabled. If you don’t, all you have to do is check the Mac Catalyst option under Deployment Info in the General settings tab for your target (see this detailed guide for specifics).

Support for Swift Package Manager is built into Xcode, which makes it easier to add dependencies to the project. PSPDFKit for Catalyst supports Swift Package Manager, and we’ll be using it for the purpose of this blog post.

Also, please make sure you’re using the latest stable version of Xcode.

Adding the PSPDFKit Swift Package

To add the PSPDFKit Swift package:

  • Go to the Package Dependencies tab of your project’s settings in Xcode.

  • Click Add Package Dependency and paste the URL below into the search field.

https://github.com/PSPDFKit/PSPDFKit-SP

  • Select Branch: master under the Dependency Rule option, and then click Add Package.

Displaying a PDF Document

PSPDFKit provides easy-to-follow and convenient APIs that let you add a document viewer to your project with just a few lines of code.

Import the PSPDFKit model and UI frameworks into your file. Be sure it contains the code specifying where you want to display the document:

import PSPDFKit
import PSPDFKitUI

Now, create a Document instance using the URL of the document you want to display, and present it in a PDFViewController:

// Creating a `Document` instance.
let fileURL = Bundle.main.url(forResource: "Document", withExtension: "pdf")!
let document = Document(url: fileURL)

// The configuration closure is optional and allows additional customization.
let pdfController = PDFViewController(document: document) {
	$0.isPageLabelEnabled = false
}

// Present the PDF view controller within a `UINavigationController` to show built-in toolbar buttons.
present(UINavigationController(rootViewController: pdfController), animated: true)

And voilà! Your Catalyst app now can display PDF documents.

Screenshot of a magazine displayed in PSPDFKit for Mac Catalyst’s document viewer.

The Case for a PSPDFKit Viewer

PSPDFKit adapts to Catalyst to provide a great macOS experience. It has support for window titles, which is a must for a document viewer on macOS. Tooltips are also supported, and these are useful in giving more information in the form of small messages. Point support is available for easier text selection, along with keyboard shortcuts and gestures for scrolling and zooming.

PSPDFKit supports plenty of features — such as text selection, annotation editing, document sharing, document editing, and signatures. By using a PDFViewController to display a PDF document, you get all of these features (some require licensing) out of the box without having to write a single line of code. PDFViewController allows users to select text from the document displayed; it also allows them to highlight text, create annotations, and more.

ℹ️ Note: Please ensure the document loaded by the Document instance is at a writable location to allow making changes to the PDF document.

Changing Appearance

You can use the PDFConfiguration API to customize the viewer to have different page modes and transitions, which in turn enables things such as the continuous scrolling of pages:

let document = ... // Document instance.
let pdfController = PDFViewController(document: document) {
	$0.pageMode = .double
	$0.pageTransition = .scrollContinuous
}

The PDF document viewer also allows you to change the appearance of pages to sepia or something darker. These options can be especially useful, as they make it easier to read in darker environments. You can achieve this with a single line of code:

let controller = PDFViewController(document: ...)
// Changing the appearance of the pages to sepia.
controller.appearanceModeManager.appearanceMode = .sepia

Reader View

Research papers and instruction manuals are now often shared in the form of PDF documents. Consuming the content from these documents can be made slightly more convenient using Reader View. This displays the contents in a single-column view that brings the focus to a single area, making it easier to read:

// Create the `Document` you want to view in Reader View.
let fileURL = Bundle.main.url(forResource: "Document", withExtension: "pdf")!
let document = Document(url: fileURL)

// Create the `ReaderViewController`.
let readerViewController = ReaderViewController(document: document)

// Present the `ReaderViewController`.
present(readerViewController, animated: true)

Conclusion

PSPDFKit for Catalyst provides simple and robust APIs to add support not only for viewing a PDF document, but also for customizing the appearance of the document for viewing. You can use the SDK to add more advanced features such as editing annotations, indexed full-text search, adding watermarks to documents, digitally signing documents, and more.

You can get started with our free trial. PSPDFKit also comes with great customer support, so please reach out to us if you have any questions about our Mac Catalyst PDF SDK.

You can also check out our PDF Viewer - Annotation Expert app — which is a Mac Catalyst app that uses the PSPDFKit for Catalyst SDK — to take some of these features for a test drive.

Related Products
Share Post
Free 60-Day Trial Try PSPDFKit in your app today.
Free Trial

Related Articles

Explore more
PRODUCTS  |  iOS • Mac Catalyst • Releases

PSPDFKit 11.5 for iOS Adds Measurement Tools

PRODUCTS  |  iOS • Android • Releases • Components

Measurement Tools — Measure Distance, Perimeter, and Area in PDFs

DEVELOPMENT  |  iOS • Insights • Xcode

Download Failures with Swift Package Manager Binary Targets