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:
- 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. Click Next.
Choose the options for your new project. Please make sure to choose Objective-C as the programming language. Then, click Next.
Place the project to the location as desired. Then, click Create.
Integrate ComPDFKit into Your Apps
There are three 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
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: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
Run
pod install
and wait for CocoaPods to download ComPDFKit.Open your application’s newly created workspace (
PDFViewer.xcworkspace
) in Xcode.
Adding the ComPDFKit Swift Package Manager Dependency
1.Open your application in Xcode and select your project’s Package Dependencies tab.
2.Copy the ComPDFKit Swift package repository URL into the search field:
https://github.com/ComPDFKit/compdfkit-pdf-sdk-apple-package
3.In the Dependency Rule fields, select Branch > master, and then click Add Package.
4.After the package download completes, select Add Package.
ComPDFKit should now be listed under Swift Package Dependencies in the Xcode Project navigator.
Adding the ComPDFKit XCFrameworks Manually
To add the dynamic xcframework "ComPDFKit.xcframework" into the "PDFViewer" project, please follows the steps below:
- Right-click the "PDFViewer" project, select Add Files to "PDFViewer"....
Find and choose "ComPDFKit.xcframework" in the download package, and then click Add.
Note: Make sure to check the Copy items if needed option.
Then, the "PDFViewer" project will look like the following picture.
- 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.
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.
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.
- 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.
Import
<ComPDFKit/ComPDFKit.h>
at the top of yourUIViewController.m
subclass implementation:objective-c#import <ComPDFKit/ComPDFKit.h>
Create a
CPDFDocument
object through NSURL, and create aCPDFView
to display it. The following code shows how to load PDF data using a local PDF path and display it byCPDFView
.objective-cNSBundle *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;
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]; }
Connect your device or simulator, and use shortcut Command_R to run the App. The PDF file will be opened and displayed.
Troubleshooting
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.
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.
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.
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.
Other Problems
If you meet some other problems when integrating our ComPDFKit PDF SDK for iOS, feel free to contact ComPDFKit team.