ios - 在 Swift 4 中使用 UIActivityViewController 共享 pdf 文件
问题描述
我正在使用 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)
显示以下结果:
我想要的是显示更多功能,例如“复制到书籍”和“添加到笔记”,如下所示:
解决方案
如果您想共享服务器上的 pdf 文件并且您有一个 URL。然后首先将该文件下载到您的设备中,然后将该文件共享给任何其他人。
如果您Alamofire
在代码中使用,则有代码。
第一阶段
import Alamofire
第 2 阶段
在您的课程中添加此功能:-
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
}
}
}
第三阶段
在您的分享按钮上添加此操作
@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)
}
}
推荐阅读
- c# - 如何在用户控件中使用命令
- android - 如何从 News API 获取完整的新闻内容
- maven - 将maven项目部署到heroku期间构建失败
- php - 给定出生日期,如何计算用户的年龄?
- c# - c# 从 Sharepoint 加载列表
- python - 我得到 __init__() 使用 CreateView 获得了一个意外的关键字参数“实例”
- java - 使用 HashMap 的值更新 Arraylist
- r - 使用标准化 Y 模拟回归数据
- java - 如何使用附加信息(如参数)在 java 中创建自定义堆栈跟踪?
- vba - Excel VBA:将唯一行从一张纸复制到另一张纸