Отправить PDF-файл в режиме просмотра веб-страниц на другие устройства, используя код swift - PullRequest
0 голосов
/ 13 мая 2019

У меня есть WKWebView, который отображает PDF.Я хочу поделиться файлом с другими устройствами, такими как iPad, iPhone .... с помощью кнопки поделиться.Я попытался отобразить PDF в предварительном просмотре, чтобы на нем была кнопка «Поделиться с iOS» с кодом ниже.

import UIKit
import WebKit

class ShowPDFView: UIViewController, UIDocumentInteractionControllerDelegate {

@IBAction func SharePDFFile(_ sender: Any) {

        let fileName = "testPDF"
        guard let urlPath = Bundle.main.url(forResource: fileName, withExtension: "pdf") else {return}
            let controller = UIDocumentInteractionController(url: urlPath)
            controller.delegate = self
            controller.presentPreview(animated: true)

    }

 func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController!) -> UIViewController! {
            return self
        }

        func documentInteractionControllerViewForPreview(controller: UIDocumentInteractionController!) -> UIView! {
            return self.view
        }

        func documentInteractionControllerRectForPreview(controller: UIDocumentInteractionController!) -> CGRect{
            return self.view.frame
        }

Я получил ошибку во время выполнения.

[MC] Reading from private effective user settings.

Предварительный просмотр не загружен.Кто-нибудь знает почему?

1 Ответ

0 голосов
/ 14 мая 2019

Эта функция работает для меня. В Свифт 4

@IBAction func SharePDFFile(_ sender: Any) {

        let fm = FileManager.default

        var pdfURL = (fm.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
        pdfURL = pdfURL.appendingPathComponent("johnMilky.pdf") as URL

        //Rename document name to "myFile.pdf"
        let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("johnMilkyFile.pdf") as NSURL

        do {
            let data = try Data(contentsOf: pdfURL)

            try data.write(to: url as URL)

            let activitycontroller = UIActivityViewController(activityItems: [url], applicationActivities: nil)
            if activitycontroller.responds(to: #selector(getter: activitycontroller.completionWithItemsHandler))
            {
                activitycontroller.completionWithItemsHandler = {(type, isCompleted, items, error) in
                    if isCompleted
                    {
                        print("completed")
                    }
                }
            }

            //activitycontroller.excludedActivityTypes = [UIActivity.ActivityType.airDrop]
            activitycontroller.popoverPresentationController?.sourceView = self.view
            self.present(activitycontroller, animated: true, completion: nil)

        }
        catch {
            print(error)
        }


    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...