Opening a PDF in Objective-C

Tutorials | Objective-C · How to · iOS Fri. 17 Jun. 2022

Opening a PDF is a fundamental function that can easily be overlooked. However, with iOS becoming a more powerful operating system and Objective-C continuously increasing in popularity, it’s necessary for us to revisit the basic developer action. Here’s how to use Objective-C to open a PDF with the built-in UIWebView in iOS apps.

 

 

With UIWebView

 

Step 1: Creating the Project

 

Xcode - File → New → Project…, choose “App”:

 


Enter the product and organization names and choose Objective-C as the language:

 


Choose the destination and create the project.

 

Step 2: Adding a PDF to the Project

 

Drag and drop a document into the newly created project. Choose to add it to your app’s target:

 


The document will show up in the file list on the left and in the “Copy Bundle Resources” build phase of your app’s target:

 


Step 3: Opening a PDF Inside of a Web View

 

We’re creating a web view in ViewController:

 

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:webView];

    NSURL *documentURL = [[[NSBundle mainBundle] resourceURL] URLByAppendingPathComponent:@"Document.pdf"];
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:documentURL];
    [webView loadRequest:urlRequest];
}

 

After finishing the several steps, we can scroll through a PDF document. But annotations, forms, and other functionalities are not allowed. These are what ComPDFKit PDF SDK can provide, with more convenient operations.

 

 

With ComPDFKit

 

Step 1: Integrating ComPDFKit into Your Apps

 

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

 


 

2. Find and choose "ComPDFKit.xcframework" in the download package, and then click Add. Make sure to check the "Copy items if needed" option.

 


 

Here’s what the "PDFViewer" project will look like:

 


 

3. 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".

 


Step 2: Displaying 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 in a simple way.

 

Then, add the following code to ViewController.m to display a PDF document. It’s really easy to present a PDF on screen. All you need is to create a CPDFDocument object and then show it with a CPDFView  object.

 

#import <ComPDFKit/ComPDFKit.h>

- (void)viewDidLoad {
    [super viewDidLoad];

    // Get the path of a PDF
    NSString *pdfPath = @"...";

    // Initialize a CPDFDocument object with the path to the PDF file
    NSURL *url = [NSURL fileURLWithPath:pdfPath];
    CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
    if (document.error && document.error.code != CPDFDocumentPasswordError) {
        return;
    }

    // Initialize a CPDFView object with the size of the entire screen
    CPDFView *pdfView = [[[CPDFView alloc] initWithFrame:self.view.bounds] autorelease];

    // Set the document to display
    pdfView.document = document;

    // Add the pdfView to the root view
    [self.view addSubview:pdfView];
}

 

Now PDF documents can be viewed in ComPDFKit. Besides, creating annotations, filling forms, manipulating pages, and plenty of other things are supported. All these can be achieved with just a few lines of code. We are committed to supporting multiple functions with simple steps. Contact us and get a free trial of our powerful SDK!

 

Ready to Get Started?

Download our all-in-one ComPDFKit for free and run it to your project within minutes!