This guide will take you through the steps necessary to integrate PSPDFKit into a newly created iOS application. By the end, you’ll be able to present a PDF document in the default PSPDFKit UI.
Requirements
-
A computer running macOS
-
The latest stable version of Carthage. You can check your version of Carthage with
carthage version
.
Creating a New Xcode Project
-
Open Xcode and select New Project in the File > New > Project… menu to create a new project for your application:
-
Choose the App template for your project:
-
When prompted, enter your app name (PSPDFKit-Demo) and your organization identifier (com.example) and choose Storyboard for the interface:
-
Click the Next button and select the location to save the project.
-
Click the Create button to finish.
Adding the PSPDFKit Carthage Dependency
![]()
For new PSPDFKit integrations, we recommend using Swift Package Manager instead of Carthage.
-
In the terminal, change the directory to the location of the
PSPDFKit-Demo.xcodeproj
file and create a Cartfile next to it:
cd path/to/xcode_project_directory cat > Cartfile
-
Open your project’s Cartfile in a text editor and add the PSPDFKit dependency:
binary "https://customers.pspdfkit.com/pspdfkit-ios.json"
-
Run
carthage update
and wait for Carthage to download PSPDFKit. -
Open your application’s project or workspace in Xcode.
-
Drag the binaries from
Carthage/Build/iOS
into the Frameworks, Libraries, and Embedded Content section of your target.
-
To work around the compatibility issue between fat frameworks and Xcode 12, make sure
EXCLUDED_ARCHS
for iOS Simulator SDK is set to arm architectures (arm64
) in your project configuration. This ensures you’ll avoid running into an issue while building for iOS Simulator:
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = arm64;
Displaying a PDF
-
Add the PDF document you want to display to your application by dragging it into your project. On the dialog that’s displayed, select Finish to accept the default integration options. You can use this QuickStart Guide PDF as an example.
-
Import
PSPDFKit
andPSPDFKitUI
at the top of yourUIViewController
subclass implementation:
import PSPDFKit import PSPDFKitUI
@import PSPDFKit; @import PSPDFKitUI;
-
Load your PDF document and display the view controller by implementing
viewDidAppear(_:)
in theViewController.swift
file like so:
override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) // Update to use your document name. 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) }
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // Update to use your document name. NSURL *documentURL = [NSBundle.mainBundle URLForResource:@"Document" withExtension:@"pdf"]; PSPDFDocument *document = [[PSPDFDocument alloc] initWithURL:documentURL]; // The configuration object is optional and allows additional customization. PSPDFViewController *pdfController = [[PSPDFViewController alloc] initWithDocument:document configuration:[PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) { builder.pageLabelEnabled = NO; }]]; // Present the PDF view controller within a `UINavigationController` to show built-in toolbar buttons. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:pdfController]; [self presentViewController:navController animated:YES completion:NULL]; }