简体   繁体   English

如何在iBooks或其他PDF可读应用程序中从WebView保存PDF文件?

[英]How to save a PDF file from WebView in iBooks or an other PDF readable app?

I have an Xcode (iPhone) app with a WebView. 我有一个带有WebView的Xcode(iPhone)应用程序。 My homepage in this WebView contains a PDF file. 我在此WebView的主页中包含一个PDF文件。 I can open this PDF file. 我可以打开这个PDF文件。 But I cannot save it. 但是我无法保存。

In normal Safari browser there are the buttons "Open in…" and "Open in "iBooks"". 在普通的Safari浏览器中,有“在...中打开”和“在iBooks中打开”按钮。 But in my WebView I can just open the PDF file. 但是在WebView中,我只能打开PDF文件。 I'm not able to save/open to/in iBooks or any other app. 我无法保存/在iBooks或任何其他应用程序中打开。 Why that? 为什么? Can somebody help me please? 有人可以帮我吗? Thanks for every answer. 谢谢你的回答。

You can download pdf file in form of NSData and save it in document directory. 您可以下载NSData格式的pdf文件并将其保存在文档目录中。

NSURL *url = [NSURL URLWithString:@"deu-net.com/Berlin-Buch/14Buch.pdf"];
NSData *dataPdf = [NSData dataWithContentsOfURL:url];

//Get path directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

//Create PDF_Documents directory
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"PDF_Documents"];
[[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil];

NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"**PUT FILENAME YOU WANT**"];

[dataPdf writeToFile:filePath atomically:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM