Skip to content
Guides

How to Make an iOS App in Objective-C with ComPDFKit

This section will help you to quickly get started with ComPDFKit PDF SDK to make an iOS app in Objective-C with step-by-step instructions, which include the following steps:

  1. Create a new iOS project in Objective-C.
  2. Integrate ComPDFKit into your apps.
  3. Apply the license key.
  4. Display a PDF document.

Create a New iOS Project in Objective-C

In this guide, we use Xcode 12.4 to create a new iOS project.

Fire up Xcode, choose File -> New -> Project..., and then select iOS -> Single View Application. Click Next.

create a New iOS Project in Objective-C

Choose the options for your new project. Please make sure to choose Objective-C as the programming language. Then, click Next.

choose Objective- C as the programming language

Place the project to the location as desired. Then, click Create.

Integrate ComPDFKit into Your Apps

There are two ways to integrate ComPDFKit PDF SDK for iOS into your apps. You can choose what works best for you based on your requirements.

Adding the ComPDFKit CocoaPods Dependency

  1. Open the terminal and go to the directory containing your Xcode project: cd .../PDFViewer.

  2. Run pod init. This will create a new Podfile next to your .xcodeproj file:

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

    diff
    # Uncomment the next line to define a global platform for your project
    # platform :ios, '9.0'
    
    target 'PDFViewer' do
      # Comment the next line if you don't want to use dynamic frameworks
      use_frameworks!
      
    +  pod 'ComPDFKit', :git => 'https://github.com/ComPDFKit/compdfkit-pdf-sdk-ios-swift.git', :tag => '1.13.0'
    
      # Pods for PDFViewer
    
    end
  4. Run pod install and wait for CocoaPods to download ComPDFKit.

  5. Open your application’s newly created workspace (PDFViewer.xcworkspace) in Xcode.

Adding the ComPDFKit XCFrameworks Manually

To add the dynamic xcframework "ComPDFKit.xcframework" into the "PDFViewer" project, please follows the steps below:

  1. Right-click the "PDFViewer" project, select Add Files to "PDFViewer"....

integrate PDF SDK into iOS Apps

Find and choose "ComPDFKit.xcframework" in the download package, and then click Add.

Note: Make sure to check the Copy items if needed option.

add the dynamic xcframework

Then, the "PDFViewer" project will look like the following picture.

add the dynamic xcframework

  1. Add the dynamic xcframework "ComPDFKit.xcframework" to the Xcode’s Embedded Binaries. Left-click the project, find Embedded Binaries in the General tab, and choose Embed & Sign.

Xcode’s Embedded Binaries

For earlier versions of Xcode (like Xcode 13), the Bitcode option might be turned on by default, which requires it to be turned off to run. The precise step to do this are illustrated as shown in the picture below.

2-7-1

Apply the License Key

Please refer to section Applying the License Key for detailed steps.

Display a PDF Document

So far, we have added "ComPDFKit.xcframework" to the "PDFViewer" project, and finished the initialization of the ComPDFKit PDF SDK. Now, let’s start building a simple 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.

2-7-2

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

    objective-c
    #import <ComPDFKit/ComPDFKit.h>
  2. 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.

    objective-c
    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;
  3. Add the created CPDFView to the view of the current controller. The sample code shows below.

    objective-c
     [self.view addSubview:pdfView];

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

    objective-c
    - (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];
    }
  4. Connect your device or simulator, and use shortcut Command_R to run the App. The PDF file will be opened and displayed.

2-7-3

Troubleshooting

  1. Bitcode

    Even when all configurations are correct, there may still be compilation errors. First, check if bitcode is disabled. In earlier versions of Xcode (such as Xcode 13), the Bitcode option may be enabled by default. It needs to be set to No in order to run the app.

  2. License

    If a License setting error occurs, ensure that the Identity (Bundle ID) setting in General matches the Bundle ID you provided when contacting us for the license. If an expired License message appears, please contact the ComPDFKit team to obtain the latest License and Key.

  3. Cannot Run on i386 Architecture Simulator

    The version of Xcode 12.5 or newer, doesn't support i386 simulators. Apple dropped the i386 after switching to ARM processors and no longer maintains i386 architecture simulators. Please use ARM simulators or x86_64 architecture simulators to test and develop your program.

    So you need to search for Excluded Architectures in Build Settings in TARGETS, and then double-click it. A pop-up window will be popped up, click the plus sign (as shown below) to add i386.

2-7-4

  1. No PDF Displayed

    Check if the special encoding is required in the path we passed in, or if the local path we passed in exists.

  2. Other Problems

    If you meet some other problems when integrating our ComPDFKit PDF SDK for iOS, feel free to contact ComPDFKit team.