在当今移动网络时代,iOS 应用程序的开发对于企业和开发人员至关重要。随着对于PDF 文件协作的需求不断增加,构建 iOS PDF viewer 和编辑器可让终端用户轻松查看和编辑 PDF 文档。在本篇文章中,我们将首先讨论整合 ComPDFKit PDF SDK 并探究如何使用它开发 iOS PDF 阅读器
此部分介绍了安装要求、安装包结构以及如何使用 ComPDFKit PDF SDK 在 Swift 中制作 iOS PDF 阅读器。
使用ComPDFKit PDF SDK进行开发需要在指定的开发环境下进行。如果您的开发环境不符合要求,ComPDFKit PDF SDK 可能无法正常工作。
对于早期版本的 Xcode(如 Xcode 13),Bitcode 选项可能被设置成默认打开,这需要关闭它才能运行。执行此操作的具体步骤如下图所示:
您可以自行从 Github上下载 iOS PDF SDK安装包并解压。您将在 SDK 包中看到以下的文件。
ComPDFKit PDF SDK for iOS 的安装包包含以下文件:
在此部分,我们分步说明了如何在 Swift 中开发 iOS 应用程序。这将帮助您快速学会使用 ComPDFKit PDF SDK,完成所有步骤,您将可以获得一个简单的应用程序,可以显示指定PDF文件的内容。在本篇文章中,我们将以 Xcode 14.0.1 为例。
由于 ComPDFKit PDF SDK for iOS 是基于 Objective-C 开发的,因此它与 Swift 项目兼容。因此,在 Swift 项目中,您需要同时导入ComPDFKit.xcframework 和 头文件。我们提供了两种在 Swift 项目中导入 SDK 的方法:
CPDFKit.setLicenseKey("LICENSE_KEY", secret: "LICENSE_SECRET")
编译并运行该项目。如果控制台输出“版本信息”,则表示License设置成功。否则,请查看本文末尾的“故障排除”部分或查看控制台中的错误日志,以快速识别并解决问题。编译并运行该项目。
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)
如果文件路径中有特殊字符,你的 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。
本文系外文翻译,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系外文翻译,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。