共享pdf文档。Swift

问题描述 投票:0回答:1

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

            }

        }


    }

}
ios swift xcode share document
1个回答
0
投票

我通过这种方式解决了我的问题

 // 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)
    }
© www.soinside.com 2019 - 2024. All rights reserved.