我需要在iOS下分享/打开一些不同的文件,例如图像到iCloud或pdf到iBooks。文本和图像有几个例子,但在其他方面没什么......
我创建了自己的UIActivityItemProvider
;简化版本:
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
}
}
并开始分享:
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)
我期待像:
这样的分享但我得到的只是:
有什么建议吗?
答案 0 :(得分:20)
**
**
我花了很多时间思考如何在从URL加载的UIWebView中共享PDF文件,例如“http://youPdfFile.pdf”
经过几天的努力,我发现了我认为最合适的方式:
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")
}
}
答案 1 :(得分:4)
此代码适用于我共享文件。当然,文件必须已经创建:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf", @"FileName"]];
NSURL *URL = [NSURL fileURLWithPath:fullPath];
NSArray *activityItems = [NSArray arrayWithObjects:URL, nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
答案 2 :(得分:1)
您必须在UIActivityViewController中传递 pdf文件网址以获取pdf支持的应用程序选项。
func sharePdf(path:URL) {
let fileManager = FileManager.default
if fileManager.fileExists(atPath: path.path) {
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [path], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
} else {
print("document was not found")
let alertController = UIAlertController(title: "Error", message: "Document was not found!", preferredStyle: .alert)
let defaultAction = UIAlertAction.init(title: "ok", style: UIAlertAction.Style.default, handler: nil)
alertController.addAction(defaultAction)
UIViewController.hk_currentViewController()?.present(alertController, animated: true, completion: nil)
}
}
答案 3 :(得分:0)
现有的答案演示了如何使用本地文件的URL共享PDF。但是,如果不想,则不必创建文件。另一种方法是使用Data
表示您的PDF文件。
假设您使用PDFKit
在UI中显示PDF。这意味着您将处理具有PDFKit.PDFDocument
方法的dataRepresentation()
类。此方法返回Data
,可以将其传递给UIActivityViewController
初始化程序。