Tutorials

Integrate ComPDFKit Objective-C PDF Library Through CocoaPods

By ComPDFKit | Wed. 31 Jul. 2024
CocoaPodsiOSObjective-C

Integrate ComPDFKit Objective-C PDF Library Through CocoaPods

 

CocoaPods is a leading dependency manager for Objective-C projects, streamlining the integration of libraries like the ComPDFKit PDF Library for iOS. Utilizing CocoaPods simplifies the process compared to manually adding SDK frameworks.

 

This tutorial will guide you through integrating the ComPDFKit Objective-C PDF SDK into your project using CocoaPods. We'll start with instructions on installing CocoaPods—if you already have it, you can skip this step. Next, you'll learn how to add ComPDFKit to your project and leverage the iOS PDF Viewer to display PDF files seamlessly.

 

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

 

 

Requirements for iOS CocoaPods Integration

 

To integrate the ComPDFKit iOS PDF library through CocoaPods, the following requirements are needed.

 

  • iOS 10.0 or higher.

  • Xcode 13.0 or newer for Objective-C.

  • CocoaPods: Follow the Step 1 to finish the CocoaPods Installation.

  • ComPDFKit iOS PDF Library: Access the ComPDFKit PDF library for iOS on GitHub.

  • License of ComPDFKit iOS PDF Library: Obtain a 30-day trial license by applying on the ComPDFKit website. It will be sent via email.

 

Note: 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.

 

 

Step 1: Install CocoaPods for iOS PDF Library Integration

 

CocoaPods is available as a Ruby gem you can install by running gem install cocoapods. Using the default Ruby available on macOS can require you to use sudo when installing CocoaPods: sudo gem install cocoapods. To avoid this, we recommend using a Ruby version manager like rbenv.

 

 

Step 2: Install & Integrate ComPDFKit iOS PDF Library

 

Open the terminal and go to the directory containing your Xcode project: cd .../PDFViewer.Run pod init. This will create a new Podfile next to your .xcodeproj file:

 

Create a new Podfile

 

Open the newly created Podfile in a text editor and add the ComPDFKit pod URL:

# Uncomment the next line to define a global platform for your project
# platform :ios, '10.0'
​
target 'PDFViewer' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!
  
+  pod "ComPDFKit", podspec:'https://www.compdf.com/download/ios/cocoapods/xcframeworks/compdfkit/2.0.0.podspec'
​
  # Pods for PDFViewer
​
end

Run pod install and wait for CocoaPods to download and integrate ComPDFKit into your project. Open your application’s newly created workspace (PDFViewer.xcworkspace) in Xcode.

 

 

Step 3: Verify the Objectve-C CocoaPods Integration

 

Open the created Xcode workspace and import ComPDFKit into any of your source files:

#import <ComPDFKit/ComPDFKit.h>

After you run the target, ComPDFKit has been successfully integrated into your project.

 

 

Step 4: Apply the License Key for Your iOS PDF 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>online</type> field. If 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>

3. 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.m, 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 verifyLicense:@"YOUR_LICENSE_KEY_GOES_HERE" completionHandler:^(CPDFKitOnlineLicenseCode code, NSString *message) {}); in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions. You need to replace the LICENSE_KEY with the license you obtained.

#import <ComPDFKit/ComPDFKit.h>
​
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Set your online license key here. ComPDFKit is commercial software.
  // Each ComPDFKit license is bound to a specific app bundle id.
  // com.compdfkit.pdfviewer
    
    [CPDFKit verifyLicense:@"YOUR_LICENSE_KEY_GOES_HERE" completionHandler:^(CPDFKitOnlineLicenseCode code, NSString *message) {
​
    }];
    
    return YES;
}

 

  • Offline license:

Follow the code below and call the method [CPDFKit verifyWithKey:@"YOUR_LICENSE_KEY_GOES_HERE"]; in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions. You need to replace the LICENSE_KEY with the license you obtained.

#import <ComPDFKit/ComPDFKit.h>
​
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Set your local license key here. ComPDFKit is commercial software.
  // Each ComPDFKit license is bound to a specific app bundle id.
  // com.compdfkit.pdfviewer
    
    [CPDFKit verifyWithKey:@"YOUR_LICENSE_KEY_GOES_HERE"];
    
    return YES;
}

 

 

Step 5: Open a PDF File with Your iOS PDF Viewer

 

The following guides are given in Objective-C to open PDF files with the iOS PDF Viewer you created just now.

 

  1. Prepare a test PDF file, and drag and drop it into the newly created PDFView project. 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.

 

an example of importing a PDF document named “Online5” into the project

 

  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 the shortcut Command_R to run the App. The PDF file will be opened and displayed.

 

2-7-3

 

 

What's More

 

Now, you have integrated the ComPDFKit iOS PDF Library through CocoaPods with Objective-C and get an iOS PDF Viewer to view PDF documents.

 

Explore further to unlock additional functionalities—such as annotating PDFs, managing forms, and implementing password protection. Whether you're developing an enterprise application or a small utility tool, ComPDFKit offers robust and scalable solutions tailored to your needs.

 

More about ComPDFKit for iOS:

  • Developer Guides: For detailed integration information.

  • Tech Support: Contact the ComPDFKit support team whenever you have problems with ComPDFKit products.

  • Purchase iOS PDF SDK Online: ComPDFKit provides Lite and Plus version iOS PDF SDK for enterprises.

  • Sales Team: To get more details and an accurate quote.

  • Stay updated with the latest improvements through our Changelog.

 

 

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