Skip to content

如何集成 ComPDFKit 使用 Objective-C 创建 iOS 应用程序

本节将为您提供逐步说明,帮助您快速开始使用 Objective-C 语言集成 ComPDFKit PDF SDK 创建 iOS 应用程序。总共包括以下步骤:

  1. 创建一个新的 iOS 项目。

  2. 将 ComPDFKit 集成到您的应用程序中。

  3. 替换许可证密钥的 XML 文件。

  4. 显示 PDF 文档。

用 Objective-C 语言创建一个新的 iOS 项目

在本指南中,我们使用 Xcode 12.4 版本的开发环境创建一个新的 iOS 项目。

打开 Xcode,选择 File -> New -> Project...,然后选择 iOS -> Single View Application。点击 Next

为您的新项目设置一些必要信息。请确保选择 Objective-C 作为开发语言。然后,点击 Next

将项目保存到所需位置。然后,点击 Create

将 ComPDFKit 集成到您的 App 中

有两种方法可以将ComPDFKit PDF SDK集成到您的应用程序中。您可以根据自己的需求选择最适合自己的方法。

添加ComPDFKit CocoaPods依赖

  1. 打开终端并转到包含 Xcode 项目的目录:``cd .../PDFViewer.

  2. 运行pod init。这将在您的文件旁边创建一个新的 Podfile .xcodeproj

    cocopod-1
  3. 在文本编辑器中打开新创建的 Podfile 并添加 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. 运行pod install并等待 CocoaPods 下载 ComPDFKit。

  5. 在 Xcode 中打开应用程序新创建的 xcworkspace( PDFViewer.xcworkspace)。

手动添加ComPDFKit XCFrameworks

  1. 右键单击 “PDFViewer” 项目,如图所示,选择 Add Files to "PDFViewer"...

    在下载包中找到并选择 “ComPDFKit.xcframework” ,然后点击 Add

    注: 确保您选中了 Copy items if needed 选项。

    然后,“PDFViewer” 项目将如下图所示。

  2. 将动态xcframework “ComPDFKit.xcframework” 添加到 Xcode 的 Embedded Binaries 中。左键单击项目,在 General 选项卡中找到 Embedded Binaries,并选择 Embed & Sign

    对于较早的 Xcode 版本(如 Xcode 13),Bitcode 选项可能默认为打开状态,但这个选项需要关闭。下面的图片中显示了如何关闭 Bitcode 选项。

应用许可证密钥

具体步骤请参考应用许可证密钥

显示 PDF 文档

到目前为止,我们已经将 “ComPDFKit.xcframework” 添加到 “PDFViewer” 项目中,并完成了 ComPDFKit PDF SDK 的初始化。现在,让我们开始使用几行代码构建一个简单的 PDF 阅读器。

  1. 准备一个测试 PDF 文件,将其拖放到新创建的 PDFView 项目中。通过这种方式,您可以使用 NSBundle 加载并预览本地 PDF 文档。下图显示了将名为“Online5”的 PDF 文档导入到项目的示例。

  2. 在您的 UIViewController.m 子类实现的顶部导入 <ComPDFKit/ComPDFKit.h>

    objective-c
    #import <ComPDFKit/ComPDFKit.h>
  3. 通过 NSURL 创建一个 CPDFDocument 对象,并创建一个 CPDFView 来显示它。下面的代码展示了如何使用本地 PDF 文件路径加载PDF数据并通过 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;
  4. 将创建的CPDFView添加到当前控制器的视图中。示例代码如下所示。

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

    这里显示的代码是上面提到的步骤的集合:

    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];
    }
  5. 连接您的设备或模拟器,使用快捷方式 Command_R 运行应用程序。PDF 文件将被打开并显示。

    2-7-3

故障排除

  1. Bitcode

    即使所有配置都正确,仍可能出现编译错误。首先,请检查是否禁用了 Bitcode。在较早的 Xcode 版本(例如 Xcode 13)中,默认情况下会启用 Bitcode 选项。它需要设置为 No 才能运行应用程序。

  2. 许可证

    如果出现许可证设置错误,请确保 General 中的 Identity(Bundle ID)设置与您在联系我们获取许可证时提供的 Bundle ID 匹配。如果出现过期的许可证消息,请联系ComPDFKit团队获取最新的许可证和密钥。

  3. 无法在 i386 架构模拟器上运行

    Xcode 12.5 或更高版本的版本,不支持i386模拟器。苹果在转换为 ARM 处理器后放弃了 i386,并且不再维护i386架构模拟器。请使用 ARM 模拟器或 x86_64 架构模拟器来测试和开发您的程序。

    所以您需要在 TARGETSBuild Settings 中搜索 Excluded Architectures,然后双击它。会弹出一个弹出窗口,点击加号(如下图所示)来添加 i386

    2-7-4

  4. 未显示 PDF

​ 检查我们传递的路径是否需要特殊编码,或者我们传递的本地路径是否存在。

  1. 其他问题

​ 如果在集成我们的 ComPDFKit PDF SDK for iOS 时遇到其他问题,请随时联系ComPDFKit团队