How to Make an iOS App in Objective-C with ComPDFKit PDF SDK
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 includes the following steps:
- Create a new iOS project in Objective-C.
- Integrate ComPDFKit into your apps.
- Apply the license key.
- 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 as shown in
Figure 2-2. Click Next.
Figure 2-2
Choose the options for your new project as shown in Figure 2-3. Please make sure to choose Objective- C as the programming language. Then, click Next.
Figure 2-3
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.
If you just want to use the default built-in Ul implementations to develop your apps for simplicity and convenience, you need to include the following files:
- ComPDFKit.xcframework - Include the ComPDFKit dynamic library (arm64_armv7, x86_64-simulator) and associated header files.
- Source files - Found in the " PDFViewer / Source " folder. They are the default built-in Ul.
- Resource files - Found in the " PDFViewer / Resources " folder. They are needed for the default built-in Ul implementations, such as images, strings, and other resources.
If you want to customize your (PDF-related) app’s Ul design, you need to include the following file:
- ComPDFKit.xcframework - Include the ComPDFKit PDF SDK dynamic library (arm64_armv7, x86_64-simulator) and associated header files.
- Resource files - Found in the " PDFViewer / Resources " folder. They are needed for the default built-in Ul implementations, such as images, strings, and other resources.
To add the dynamic xcframework "ComPDFKit.xcframework" into the "PDFViewer" project, please follows the steps below:
- Right-click the "PDFViewer" project, and select Add Files to "PDFViewer"...as shown in Figure 2-4.
Figure 2-4
- Find and choose "ComPDFKit.xcframework" in the download package, and then click Add as shown in Figure 2-5.
Note: Make sure to check the "Copy items if needed" option.
Figure 2-5
Then, the "PDFViewer" project will look like Figure 2-6.
Figure 2-6
- 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" as shown in Figure 2-7.
Figure 2-7
Apply the License Key
It is important that you set the license key before using any ComPDFKit PDF SDK classes.
// objective-c
#import <ComPDFKit/ComPDFKit.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Set your license key here. ComPDFKit is commercial software.
// Each ComPDFKit license is bound to a specific app bundle id.
// com.compdfkit.pdfviewer
[CPDFKit setLicenseKey:@"YOUR_LICENSE_KEY_GOES_HERE"
secret:@"YOUR_LICENSE_SECRET_GOES_HERE"];
return YES;
}
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.
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.
// objective-c
#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];
}