分享PDF文档。Swift

3

我正在尝试分享在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()

            }

        }


    }

}

你在哪里为 nameFile 设置值?在当前代码中它是一个空字符串。 - koen
当我从表格切换到这个视图时,我将其传递。 - MastIos
3个回答

13

您还可以使用 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)
    }
}

6

我是这样解决我的问题的

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

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

        // Create the Array which includes the files you want to share
        var filesToShare = [Any]()

        // Add the path of the file to the Array
        filesToShare.append(fileURL)

        // Make the activityViewContoller which shows the share-view
        let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)

        // Show the share-view
        self.present(activityViewController, animated: true, completion: nil)
    }

0
更简洁的代码
// Share doc
func shareDocAction() {

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

    // Make the activityViewContoller which shows the share-view
    let activityViewController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)

    // Show the share-view
    self.present(activityViewController, animated: true)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接