ios - 在 Swift 4 中使用 UIActivityViewController 共享 pdf 文件

标签 ios swift uiactivityviewcontroller

我正在使用 UIActivityViewController 来共享一个 PDF 文件:

let pdfFilePath = URL(string: "https://www.tutorialspoint.com/swift/swift_tutorial.pdf")
let pdfData = NSData(contentsOf: pdfFilePath!)
let activityVC = UIActivityViewController(activityItems: [pdfData!], applicationActivities: nil)

present(activityVC, animated: true, completion: nil)

显示以下结果:

enter image description here

我想要的是显示更多功能,例如“复制到书籍”和“添加到笔记”,如下所示:

enter image description here

最佳答案

如果您想共享服务器上的 pdf 文件并且您有一个 URL。然后首先在您的设备中下载该文件,然后将该文件共享给任何其他人。

如果您使用 Alamofire在你的代码中有代码。

钢带 1

import Alamofire

磁带 2

在您的类(class)中添加此功能:-
func downloadPdf(downloadUrl : String, fileName: String, completionHandler:@escaping(String, Bool)->()){

        let destinationPath: DownloadRequest.DownloadFileDestination = { _, _ in
            let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
            let fileURL = documentsURL.appendingPathComponent("\(fileName).pdf")
            return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
        }
        print(downloadUrl)
        Alamofire.download(downloadUrl, to: destinationPath)
            .downloadProgress { progress in

            }
            .responseData { response in
                print("response: \(response)")
                switch response.result{
                case .success:
                    if response.destinationURL != nil, let filePath = response.destinationURL?.absoluteString {
                        completionHandler(filePath, true)
                    }
                    break
                case .failure:
                    completionHandler("", false)
                    break
                }
        }
    }

钢带 3

在您的分享按钮上添加此操作
@IBAction func btnShareAction(_ sender: UIButton) {

        let myURL = "http://www.demo.com/demo.pdf"  // change this with your URL
        self.downloadPdf(downloadUrl : myURL, fileName: "invoice") { (localFileUrl, bool) in

             let fileURL = NSURL(fileURLWithPath: localFileUrl)
            let activityViewController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
            self.present(activityViewController, animated: true, completion: nil)

          }
      }

关于ios - 在 Swift 4 中使用 UIActivityViewController 共享 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54459961/

相关文章:

ios - 固定背景图片

ios - 从 iOS 中的 UIActivityController 选项中删除注释

swift - 通过本地 url 共享图像时,Instagram 共享扩展返回错误消息

ios - Kiwi 单元测试在 Xcode 中从未失败

ios - tableviewCell的字幕样式添加UISwitch

objective-c - If-Else 语句不起作用

ios - 适用于 iOS 和 libc++ 的 Google Maps SDK

swift - 如何在 swift 中对 UIAlertController 的文本字段进行自动完成

swift - UIActivityviewcontroller 中的文本不可编辑

ios - swift (RxSwift) : Using generics to link ViewItem and Cell classes