我正在使用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
在你的代码中,那么就有代码。
第1阶段
import Alamofire
Stape 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
}
}
}
第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)
}
}