In today's mobile-first world, creating an iOS app is a must-have for businesses and developers alike. And, with the increasing need to work with PDF documents, building an iOS PDF viewer and editor with ComPDFKit, a powerful PDF SDK, enable your end users to view and edit PDFs with ease.
In this blog, we'll start by exploring the necessary steps to integrate the ComPDFKit PDF SDK and build an iOS PDF Reader with ComPDFKit.
Get Started with ComPDFKit iOS PDF SDK
ComPDFKit is a powerful PDF SDK. It is easy to embed ComPDFKit PDF SDK in your iOS application with a few lines of Objective-C code. Take just a few minutes and get started.
The following sections introduce the requirements, structure of the installation package, and how to make an iOS PDF Reader in Objective-C with ComPDFKit PDF SDK.
Requirements
The following developing environment is required to develop programs with ComPDFKit PDF SDK. ComPDFKit PDF SDK may not work when your developing environment is lower.
- Device System: iOS 10.0 or higher.
- IDE Version: Xcode 12.0 or newer. We take Xcode 14.0.1 as an example in this blog.
- Currently, running on Apple Silicon emulator is not supported on Mac’s M series chips.
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 steps to do this are illustrated in the figure below:
iOS Package Structure
You can contact us to access our PDF SDK installation package. Download and decompress the package of ComPDFKit PDF SDK for iOS. You will see all the following files in the SDK package.
- ComPDFKit.xcframework - Include the ComPDFKit dynamic library (arm64_armv7, x86_64-simulator) and associated header files.
- PDFViewer - A folder containing iOS (Objective-C) sample projects.
- PDFViewer-Swift - A folder containing Swift (iOS) sample projects.
- api_reference_ios - API reference.
- developer_guide_ios.pdf - Developer guide.
- release_notes.txt - Release information.
- legal.txt - Legal and copyright information.
Make an iOS PDF Viewer in Objective-C
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. Through the following steps, you will get a simple application that can display the contents of a specified PDF file. We take Xcode 14.0.1 as an example in this blog.
Step 1: Create a New Project
1. Fire up Xcode, and choose File-> New -> Project. Then, select App in iOS -> Application. And then, click Next.
2. Choose the options for your new project:
- Enter the name of your product, for example, PDFViewer.
- Choose and enter the Apple developer account of the app you want to release.
- Enter the name of your organization identifier (com.example.pdfviewer).
- Choose Storyboard for the interface.
- Choose the programming Language "Objective-C".
- Click the Next button.
- Place the project to the location as desired. Then, click Create.
Step 2: Add ComPDFKit PDF SDK Package
1. Find the ComPDFKit.xcframework in the ComPDFKit PDF SDK package. It includes all binary files for architectures.
2. Open the newly created iOS project and select General in the panel on the right. Then locate the section of Frameworks, Libraries, and Embedded Content, and drag the ComPDFKit.xcframework directly into this section, and set the Embed option to Embed & Sign.
3. Use the shortcut "Command_B" to build the project. If there is no errors reported, it means the configuration is correct and you can proceed to the next step. If some errors are reported, you need to check the reason for the errors. If you cannot identify the errors, you can contact our technical team to solve the issues.
Step 3: Apply the License Key
1. Import the header file ComPDFKit/ComPDFKit.h to AppDelegate.
2. Follow the code below and call the method CPDFKit setLicenseKey:@"LICENSE_KEY" SECRET:@"LICENSE_SECRET"
in didFinishLaunchingWithOptions
. You need to replace the LICENSE_KEY and LICENSE_SECRET with the license you obtained.
#import <ComPDFKit/ComPDFKit.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
/*
// Set your license key here. ComPDFKit is commercial software.
// Each ComPDFKit license is bound to a specific app bundle id.
// Notice: This is a demo project, presenting completed ComPDFKit functions.
// The functions might be different based on the license you have purchased.
// Please check the functions you chose work fine in this demo project.
// BOOL tIsFeatureLocked = ![[CPDFKit sharedInstance] allowsFeature:CPDFKitFeatureSecurityWatermark];
*/
// [CPDFKit setLicenseKey:@"YOUR_LICENSE_KEY_GOES_HERE" secret:@"YOUR_LICENSE_SECRET_GOES_HERE"];
[CPDFKit setLicenseKey:@"" secret:@""];
NSString *tAnnotateAuther = CPDFKitShareConfig.annotationAuthor;
NSLog(@"CPDFKit Annotation Author: \t %@", tAnnotateAuther);
CPDFKitShareConfig.enableAnnotationNoRotate = YES;
return YES;
}
Compile and run the project. If the console outputs "version information", it means that the License has been set successfully. Otherwise, please check the "Troubleshooting" section at the end of this blog or check error logs in the console to quickly identify and solve the issue.
Step 4: Display a PDF Document
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. 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
.
NSString *pdfPath= [bunle pathForResource:@"Online5" ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:pdfPath];
CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
CGRect rect = self.view.bounds;
CPDFView *pdfView = [[[CPDFView alloc] initWithFrame:rect] autorelease];
pdfView.document = document;
3. Add the created CPDFView
to the view of the current controller. Sample code shows below.
[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.
5. If the PDF content cannot be displayed, check if the created NSURL and CPDFDocument
objects are empty, or if the size of the created CPDFView
is zero. They should not be empty.
If there are special characters in the file path, your NSURL will be nil. Handle it with the following code.
[pdfPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
If the size of the created CPDFView is zero, follow the code below to adjust the size of View.
pdfview.frame = self.view.frame.bounds
All the code to display a PDF file shows below:
NSString *pdfPath= [bunle pathForResource:@"Online5" ofType:@"pdf"];
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];
Troubleshooting
1. Bitcode
Even when all configurations are correct, there may still be compilation errors. First, check if bitcode is disabled. In older 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. 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.
4. Other Problems
If you meet some other problems when integrating our ComPDFKit PDF SDK for iOS, feel free to contact ComPDFKit team.