ios - 分享 pdf 文档 .Swift

标签 ios swift xcode share document

我正在尝试共享在 pdfView 中打开的 pdf 文档。但由于某种原因,activityViewController 是空的,我无法将文件发送到某处。 请告诉我如何修复我的代码!

我的ViewController的代码

pdfView.swift

import UIKit
import PDFKit

var nameFile:String?
var titleChapter:String?

class pdfView: UIViewController {

    @IBOutlet weak var pdfDocView: PDFView!

    // Share doc
    @IBAction func shareDocAction(_ sender: UIBarButtonItem) {

        let path = Bundle.main.path(forResource: nameFile,  ofType:"pdf")
        let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path!))

        var filesToShare = [Any]()
        filesToShare.append(pdfDocument!)

        let activityViewController = UIActivityViewController(activityItems: filesToShare , applicationActivities: nil)
        activityViewController.popoverPresentationController?.sourceView = self.view
        present(activityViewController, animated: true, completion: nil)
    }

    //Pdf view
    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = titleChapter
        navigationController?.navigationBar.barTintColor = .white

        //name Documents for view
        if let path = Bundle.main.path(forResource: nameFile,  ofType:"pdf") {
            if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
                pdfDocView.displayMode = .singlePageContinuous
                pdfDocView.autoScales = true
                pdfDocView.displayDirection = .vertical
                pdfDocView.document = pdfDocument
                pdfDocView.canZoomIn()
                pdfDocView.canZoomOut()

            }

        }


    }

}

最佳答案

您还可以使用 dataRepresentation 共享 PDFDocument,如下所示:

func sharePDF(_ filePDF: PDFDocument) {
    if let pdfData = filePDF.dataRepresentation() {
        let objectsToShare = [pdfData]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        activityVC.popoverPresentationController?.sourceView = self.button

        self.present(activityVC, animated: true, completion: nil)
    }
}

关于ios - 分享 pdf 文档 .Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59061248/

相关文章:

objective-c - xcode构建失败没有解释

ios - 打开设置应用程序

ios - 多次调用 estimatedHeightForRowAtIndexPath 导致不稳定的 uitableview

ios - ViewController 没有取消

ios - FlatMap 对 Int 类型数组发出警告,但对 String 类型数组则不发出警告

ios - 如何让ScrollView半透明且背后内容交互?

iphone - Xcode : self-adjusting font size depending on how a much text is in a text field

iphone - 有没有办法让 PayPal 网页 View 旋转为横向模式?

ios - Isfirstresponder 快速问题

iOS 企业内部分发流程