在当今移动网络时代,iOS 应用程序的开发对于企业和开发人员至关重要。随着对于PDF 文件协作的需求不断增加,构建 iOS PDF viewer 和编辑器可让终端用户轻松查看和编辑 PDF 文档。在本篇文章中,我们将首先讨论整合 ComPDFKit PDF SDK 并探究如何使用它开发 iOS PDF 阅读器
ComPDFKit iOS PDF SDK 入门
此部分介绍了安装要求、安装包结构以及如何使用 ComPDFKit PDF SDK 在 Swift 中制作 iOS PDF 阅读器。
安装要求
使用ComPDFKit PDF SDK进行开发需要在指定的开发环境下进行。如果您的开发环境不符合要求,ComPDFKit PDF SDK 可能无法正常工作。
设备系统:iOS 10.0或更高版本。
-
IDE 版本:Xcode 12.0 或更高版本。在本篇文章中,我们将以 Xcode 14.0.1 为例。
image.png 目前,Mac 的 M 系列芯片不支持在 Apple Silicon 模拟器上运行。
对于早期版本的 Xcode(如 Xcode 13),Bitcode 选项可能被设置成默认打开,这需要关闭它才能运行。执行此操作的具体步骤如下图所示:

iOS 安装包结构
您可以自行从 Github上下载 iOS PDF SDK安装包 并解压。您将在 SDK 包中看到以下的文件。
ComPDFKit PDF SDK for iOS 的安装包包含以下文件:
- ComPDFKit.xcframework - 包含 ComPDFKit.xcframework 动态库 (arm64_armv7, x86_64-simulator,arm64_x86_64-maccatalyst) 和相关的头文件。
- PDFViewer - 一个集成了以上所有功能的多功能 PDF 程序。
- PDFViewer-Swift - 一个集成了以上所有功能 Swift 语言的多功能 iOS PDF 程序。
- api_reference_ios - API 接口参考文档。
- developer_guide_ios.pdf - 开发者指南。
- release_notes.txt - 版本跟新信息。
- legal.txt - 法律和版权申明。

在 Swift 中开发 iOS PDF 查看器或编辑器
在此部分,我们分步说明了如何在 Swift 中开发 iOS 应用程序。这将帮助您快速学会使用 ComPDFKit PDF SDK,完成所有步骤,您将可以获得一个简单的应用程序,可以显示指定PDF文件的内容。在本篇文章中,我们将以 Xcode 14.0.1 为例。
第 1 步:创建一个新项目
- 启动 Xcode,然后选择File -> New -> Project。然后,在iOS中选择App ->应用程序。然后,单击“Next”。

- 为新项目填写相关信息:
输入产品名称,例如 PDFViewer。
选择并输入您要发布的应用程序的Apple开发者帐户。
输入您的组织标识符的名称 (com.example.pdfviewer)。
为界面选择情节提要。
选择编程语言“Swift”。
单击“下一步”按钮。
将项目放置到所需的位置。然后,单击“创建”。

第 2 步:添加ComPDFKit PDF SDK包
- 在ComPDFKit PDF SDK包中找到ComPDFKit.xcframework 。它包括架构的所有二进制文件。
- 打开新建的iOS项目,在右侧面板中选择General。然后找到Frameworks、Libraries和Embedded Content部分,并将ComPDFKit.xcframework直接拖到此部分中,并将Embed选项设置为Embed & Sign。
- 使用快捷方式“Command_B”构建项目。如果没有报错,则说明配置正确,可以进行下一步。如果报错,需要检查错误原因。如果您无法识别错误,您可以联系我们的技术团队来解决问题。
image.png
第 3 步:将 Objective-C 导入 Swift
由于 ComPDFKit PDF SDK for iOS 是基于 Objective-C 开发的,因此它与 Swift 项目兼容。因此,在 Swift 项目中,您需要同时导入ComPDFKit.xcframework 和 头文件。我们提供了两种在 Swift 项目中导入 SDK 的方法:
-
第一种:添加一个名为 MyProjectName-Bridging-Header.h 的头文件。这将是您导入任何 Objective-C 代码所需要的唯一的头文件,以便您的 Swift 代码可以访问。然后,在项目构建设置中找到 Swift Compiler - Code Generation 部分。将您的桥接头文件的路径添加到 Objective-C Bridging Header 旁边,路径从项目根文件夹开始。它应该是:MyProject/MyProject-Bridging-Header.h。
image.png
- 第二种:导入模块。找到 “ComPDFKit.xcframework” 文件夹 -> “ios-arm64_armv7” -> “ComPDFKit.framework” -> “Modules” 。然后,整体导入 “Modules” 文件夹,或者只导入 “Modules” 文件夹中的 “Module” 文件。详情请参阅下图。

第 4 步:应用许可证密钥
- 将头文件ComPDFKit/ComPDFKit.h导入到AppDelegate中。
- 按照下面的代码,您需要将LICENSE_KEY和LICENSE_SECRET替换为您获得的许可证。
CPDFKit.setLicenseKey("LICENSE_KEY", secret: "LICENSE_SECRET")
编译并运行该项目。如果控制台输出“版本信息”,则表示License设置成功。否则,请查看本文末尾的“故障排除”部分或查看控制台中的错误日志,以快速识别并解决问题。编译并运行该项目。
第 5 步:显示 PDF 文档
- 准备一个测试PDF文件,将其拖放到新创建的pdfView项目中。这样,您就可以使用NSBundle加载,并预览本地 PDF 文档了 NSBundle。下图显示了将名为“Online5”的 PDF 文档导入到项目中作为示例。

- 通过URL创建一个CPDFDocument对象,并创建一个CPDFView来显示它。下面的代码展示了如何使用本地 PDF 路径加载 PDF 数据并通过CPDFView显示数据示例代码
let document = CPDFDocument(url: url)
if let error = document?.error, error._code != CPDFDocumentPasswordError {
return
}
let pdfView = CPDFView(frame: view.bounds)
pdfView.document = document
- 将创建的 CPDFView 添加到当前控制器的视图中。示例代码如下。
view.addSubview(pdfView)
- 连接您的设备或模拟器,然后使用快捷方式“Command+R”运行应用程序。 打开PDF 文件并显示。

- 如果无法显示PDF内容,请检查创建的NSURL和对象是否为空,或者创建的CPDFView的大小是否为零。它们不应该是空的。
如果文件路径中有特殊字符,你的 NSURL 将为 nil。用下面的代码来处理它。
filePath.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
如果创建的大小 CPDFView 为零,请按照下面的代码调整View的大小。
pdfView.frame = self.view.bounds
创建 CPDFViewController 的所有代码如下所示:
import UIKit
import ComPDFKit
class ViewController: UIViewController {
public var filePath: String = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let url = URL(filePath: filePath)
// let url = URL(fileURLWithPath: fliePath)
let document = CPDFDocument(url: url)
if let error = document?.error, error._code != CPDFDocumentPasswordError {
return
}
let pdfView = CPDFView(frame: view.bounds)
pdfView.document = document
view.addSubview(pdfView)
}
}
SceneDelegate 示例代码
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)
{
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
let fileURL = Bundle.main.bundleURL.appendingPathComponent("xxxx.pdf")
// let writbaleURL = copyFileURLToDocumentFolder(fileURL)
let writbaleURL = fileURL.path()
let tPDFViewController:ViewController = ViewController.init()
tPDFViewController.fliePath = writbaleURL
window!.rootViewController = UINavigationController(rootViewController: tPDFViewController)
window!.makeKeyAndVisible()
guard let _ = (scene as? UIWindowScene) else { return }
}
func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}
func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}
func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}
}
注意:我们在 iPhone 14 Pro iOS16.2 模拟器中运行此示例代码显示 PDF。


