使用UIActivityViewController和UIActivityItemProvider共享PDF [英] Use of UIActivityViewController and UIActivityItemProvider to share PDF
问题描述
我需要在iOS下共享/打开一些不同的文件,例如图片到iCloud或pdf到iBooks.文本和图像周围有几个示例,而其他方面则没有...
I need to share/open some different files under iOS e.g. an image to iCloud or an pdf to iBooks. There are several examples around for text and images but nothing for somewhat else...
我创建了自己的UIActivityItemProvider
;简化的版本在这里:
I created my own UIActivityItemProvider
; simplified version here:
class MyItemProvider: UIActivityItemProvider {
override func item() -> AnyObject {
self.placeholderItem
}
override func activityViewController(activityViewController: UIActivityViewController, dataTypeIdentifierForActivityType activityType: String?) -> String {
return kUTTypePDF as String
}
func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return self.placeholderItem
}
}
并开始与以下内容共享:
And start sharing with something like:
let myPDF = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("testpdf", withExtension: "pdf")) {
let activityVC = UIActivityViewController(activityItems: [MyItemProvider(placeHolder: myPDF)], applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
我期望像这样的共享对话
I expect a sharedialog like:
但是我得到的只是:
有什么建议吗?
推荐答案
**
**
我花了很多时间思考如何在从URL加载的UIWebView中共享PDF文件,例如" http://youPdfFile.pdf "
I've spent a lot of time thinking ways to share a PDF file within UIWebView loaded from a URL, like "http://youPdfFile.pdf"
几天后,我发现了我认为最合适的方法:
After a coupe of days, I've found what I think it's the most suitable way:
1)PDF必须首先保存在文件系统中.例如,可以在加载UIWebView之后完成:
class yourViewController: UIViewController{
var documentPathToLoad = "http://youPdfFile.pdf"
...
func webViewDidFinishLoad(_ webView: UIWebView) {
if yourWebView.isLoading {
// still loading
return
}
//Save the pdf document to file system
savePdf()
}
func savePdf(){
let fileManager = FileManager.default
let paths = (NSSearchPathForDirectoriesInDomain((.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("documento.pdf")
let pdfDoc = NSData(contentsOf:URL(string: documentPathToLoad)!)
fileManager.createFile(atPath: paths as String, contents: pdfDoc as Data?, attributes: nil)
}
}
2)读取保存的文件,然后共享.此方法在Swift 3.0.1中对我有效:
func loadPDFAndShare(){
let fileManager = FileManager.default
let documentoPath = (self.getDirectoryPath() as NSString).appendingPathComponent("documento.pdf")
if fileManager.fileExists(atPath: documentoPath){
let documento = NSData(contentsOfFile: documentoPath)
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [documento!], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView=self.view
present(activityViewController, animated: true, completion: nil)
}
else {
print("document was not found")
}
}
这篇关于使用UIActivityViewController和UIActivityItemProvider共享PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!