Tutorials

Build iOS PDF Reader — ComPDFKit Swift Code Example

By ComPDFKit | Wed. 31 Jul. 2024
iOSSwift

Build iOS PDF Reader — ComPDFKit Swift Code Example

 

 

If you are looking for a Swift code example to start your iOS PDF app building, here are the Swift code examples and guides. This tutorial will show you how to use the PDF Reader Swift Library of ComPDFKit for building a Swift PDF reader.

 

 

Windows   Web   Android   iOS   Mac   Server   React Native   Flutter   Electron
30-day Free

 

 

Requirements Before Using Swift Code Example

 

Here are the Using Requirements of ComPDFKit Swift Code Example. Integrating ComPDFKit requires the latest stable version of Xcode available at the time the release was made. This is a hard requirement, as each version of Xcode is bundled with a specific version of the iOS Base SDK, which often defines how UIKit and various other frameworks behave.

 

 

 

 

Download Swift Library of iOS PDF Reader

 

To follow the code example guides in Swift, you need to download the ComPDFKit PDF Library for iOS in Swift. The license in the license_key_ios.xml file is only used for the SDK demo. If you need to try to integrate the Swift PDF Library into your project, remember to get the license in the next part.

 

 

Here is the Swift SDK structure:

 

Apple PDF Library Package Structure of ComPDFKit

 

 

Get the License for ComPDKit Swift PDF Library

 

Get the license of ComPDFKit Swift Library on the ComPDFKIt website. It is the license for you to integrate the Swift PDF library into your project. Choose and apply for the license as image below. Then, an email containing the license will be sent to you.

 

License for ComPDFKit iOS SDK

 

 

Step 1: Add the Swift Package Manager Dependency

 

Swift Package Manager (SwiftPM or SPM for short) is an official tool provided by Apple for managing source code distribution, used for managing Swift code distribution. You can use it to integrate the Swift library of ComPDFKit into your project with our Swift code example.

 

Open your application in Xcode and select your project’s Package Dependencies tab.

 

project’s Package Dependencies - ComPDFKit Swift PDF Library

 

Copy the ComPDFKit Swift package repository URL into the search field:

https://github.com/ComPDFKit/compdfkit-pdf-sdk-apple-package

In the Dependency Rule fields, select Branch > master, and then click Add Package.

 

the Dependency Rule fields - ComPDFKit Swift PDF Library

After the package download completes, select Add Package.

 

Add ComPDFKit Swift PDF Library Package

ComPDFKit should now be listed under Swift Package Dependencies in the Xcode Project navigator.

 

 

Step 2: Verify the Swift PDF Library Integration

 

Open the newly created Xcode workspace and import both ComPDFKit into any of your source files. Here is the Swift code example to verify your Swift PDF Library integration:

import ComPDFKit

 

 

Step 3: Apply the License Key for Your Swift PDF Reader Project

 

Find the License Key

Accurately obtaining the license key is crucial for the application of the license.

 

  1. In the email you received, locate the XML file containing the license key.

  2. Open the XML file and determine the license type through the <type> field. If <type>online</type> present, it is an online license; if absent or <type>offline</type> is present, it is an offline license.

 

Online License:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<license version="1">
    <platform>ios</platform>
    <starttime>xxxxxxxx</starttime>
    <endtime>xxxxxxxx</endtime>
    <type>online</type>
    <key>LICENSE_KEY</key>
</license>

 

Offline License:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<license version="1">
    <platform>ios</platform>
    <starttime>xxxxxxxx</starttime>
    <endtime>xxxxxxxx</endtime>
    <key>LICENSE_KEY</key>
</license>

  1. Copy the LICENSE_KEY from the <key>LICENSE_KEY</key> field.

 

Apply the License Key

Go to the ComPDFKit official website's Online Sales Interface to submit a trial application and receive an immediate free trial license for iOS platforms, valid for 30 days. Before using any ComPDFKit PDF SDK classes, you must perform the following steps to apply the license to your application:

 

  1. In AppDelegate.swift, import the header file ComPDFKit.

  2. Depending on the type of authentication obtained in the previous step, whether online or offline, initialize the license using the respective method based on your requirements.

  3. Initialize the license:

 

  • Online license:

Follow the code below and call the method CPDFKit.verify(withOnlineLicense: "LICENSE_KEY") { code, message in } in func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool. You need to replace the LICENSE_KEY with the license you obtained.

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  // Set your online license key here. ComPDFKit is commercial software.
  // Each ComPDFKit license is bound to a specific app bundle id.
  // com.compdfkit.pdfviewe
    
    CPDFKit.verify(withOnlineLicense: "YOUR_LICENSE_KEY_GOES_HERE") { code, message in
    }
}

 

  • Offline license:

Follow the code below and call the method CPDFKit.verifyWithKey:"LICENSE_SECRET" in func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool. You need to replace the LICENSE_KEY with the license you obtained.

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  // Set your license key here. ComPDFKit is commercial software.
  // Each ComPDFKit license is bound to a specific app bundle id.
  // com.compdfkit.pdfviewer
    
    CPDFKit.verify(withKey: "YOUR_LICENSE_KEY_GOES_HERE")
    return true
}

 

 

Display a PDF Document with Swift PDF Reader

 

So far, we have added "ComPDFKit.xcframework" to the "PDFViewer" project, and finished the initialization of the ComPDFKit Swift PDF SDK. Now, let’s start building a simple iOS PDF viewer with just a few lines of code.

  1. Prepare a test PDF file, drag and drop it into the newly created PDFView project. By this way, you can load and preview the local PDF document using NSBundle. The following image shows an example of importing a PDF document named “Online5” into the project.

 

ios_2-7-2

 

  1. Import <ComPDFKit/ComPDFKit.h> at the top of your UIViewController.m subclass implementation:

#import <ComPDFKit/ComPDFKit.h>
  1. Create a CPDFDocument object through NSURL, and create a CPDFView to display it. The following code shows how to load PDF data using a local PDF path and display it by CPDFView.

NSBundle *bundle  = [NSBundle mainBundle];
NSString *pdfPath= [bundle pathForResource:@"Online5" ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:pdfPath];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
​
CGRect rect = self.view.bounds;
CPDFView *pdfView = [[CPDFView alloc] initWithFrame:rect];
pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
pdfView.document = document;
​
  1. Add the created CPDFView to the view of the current controller. The sample code shows below.

 [self.view addSubview:pdfView];

The code shown here is a collection of the steps mentioned above:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSBundle *bundle  = [NSBundle mainBundle];
    NSString *pdfPath= [bundle pathForResource:@"Online5" ofType:@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:pdfPath];
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
​
    CGRect rect = self.view.bounds;
    CPDFView *pdfView = [[CPDFView alloc] initWithFrame:rect];
    pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    pdfView.document = document;
    [self.view addSubview:pdfView];
}
  1. Connect your device or simulator, and use shortcut Command_R to run the App. The PDF file will be opened and displayed.

 

ios_2-7-3

 

 

Swift Code Example Support

 

ComPDFKit has a professional R&D team that produces comprehensive technical documentation and guides to help developers. Also, you can get an immediate response when reporting your problems to our support team.

 

  • Detailed Swift PDF library Guides: Guide on integrating all PDF features.

  • Swift Samples: Call the API of ComPDFKit PDF SDK with preset parameters, documentation, and modular swift code examples.

  • Changelog of ComPDFKit Swift Library: Check all the changes we made in every Swift PDF library version.

  • ComPDFKit Swift API Reference: Find all Class References, Protocol References, and Constant References.

  • For technical assistance, please reach out to our Technical Support.

  • To get more details and an accurate quote, please contact our Sales Team.

 

 

Windows   Web   Android   iOS   Mac   Server   React Native   Flutter   Electron
30-day Free