You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将UITableView生成的PDF导入iBooks、发送邮件及本地存储?

关于UITableView生成PDF并在iBooks中打开的问题解答

问题1:能否将该PDF保存到平板本地?

当然可以!你当前的代码已经把PDF保存到了App的文档目录,这本身就是平板本地存储的一部分。不过要注意,每个App的文档目录是沙箱隔离的,其他App默认无法直接访问,但你可以通过系统提供的分享、导入功能,让用户把文件转移到平板的其他位置(比如iCloud、文件App或者iBooks)。

问题2:将PDF在iBooks中打开的最佳方式是什么?

你当前用itms-books://的方式是错误的——这个URL scheme主要用于打开iBooks Store,不是打开本地文件。最佳方案是使用UIDocumentInteractionController,它能让系统处理文件的打开操作,包括调用iBooks。

修正后的代码示例:

首先在ViewController中声明一个UIDocumentInteractionController的属性(必须保持引用,否则会被提前释放):

var docInteractionController: UIDocumentInteractionController?

然后修改你的export方法:

@IBAction func export() {
    let pdfFilePath = self.tableView.exportAsPdfFromTable()
    guard let pdfUrl = URL(fileURLWithPath: pdfFilePath) else {
        print("无效的PDF路径")
        return
    }
    
    // 初始化文档交互控制器
    docInteractionController = UIDocumentInteractionController(url: pdfUrl)
    docInteractionController?.delegate = self
    // 指定PDF文件的UTI类型,帮助系统识别可打开的应用
    docInteractionController?.uti = "com.adobe.pdf"
    // 弹出菜单让用户选择打开方式(包含iBooks)
    docInteractionController?.presentOptionsMenu(from: self.view.bounds, in: self.view, animated: true)
    
    // 如果想直接打开iBooks(跳过选择菜单),可以用下面的代码:
    // docInteractionController?.open(in: nil, options: [:])
}

别忘了让ViewController遵守UIDocumentInteractionControllerDelegate协议:

extension YourViewController: UIDocumentInteractionControllerDelegate {
    // 必须实现这个方法,指定预览菜单的父视图控制器
    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
        return self
    }
}

问题3:有没有办法将文档目录中生成并保存的PDF导入iBooks或通过邮件发送?

当然有,两种常用方式供你选择:

方式一:使用UIActivityViewController(系统分享菜单)

这是最便捷的方式,用户可以直接选择“添加到iBooks”“邮件”等选项:

@IBAction func exportAndShare() {
    let pdfFilePath = self.tableView.exportAsPdfFromTable()
    guard let pdfUrl = URL(fileURLWithPath: pdfFilePath) else {
        print("无效的PDF路径")
        return
    }
    
    let activityViewController = UIActivityViewController(activityItems: [pdfUrl], applicationActivities: nil)
    // iPad上需要指定分享菜单的弹出位置
    if let popoverController = activityViewController.popoverPresentationController {
        popoverController.sourceView = self.view
        popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
        popoverController.permittedArrowDirections = []
    }
    self.present(activityViewController, animated: true, completion: nil)
}

打开菜单后,用户就能一键把PDF发送邮件,或者导入到iBooks、文件App等位置。

方式二:通过UIDocumentInteractionController直接打开导入菜单

如果你想只显示能打开PDF的应用(比如iBooks),可以用presentOpenInMenu方法:

docInteractionController?.presentOpenInMenu(from: self.view.bounds, in: self.view, animated: true)

补充小提示

  • 测试时,若想在模拟器的“文件”App中找到生成的PDF,需要在App的Info.plist里添加UIFileSharingEnabled和LSSupportsOpeningDocumentsInPlace并设为YES,这样你的App会显示在文件App的“我的iPhone/iPad”列表中。

内容的提问来源于stack exchange,提问作者Toto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 06:34:44