0

I have an Xcode (iPhone) app with a WebView. My homepage in this WebView contains a PDF file. I can open this PDF file. But I cannot save it.

In normal Safari browser there are the buttons "Open in…" and "Open in "iBooks"". But in my WebView I can just open the PDF file. I'm not able to save/open to/in iBooks or any other app. Why that? Can somebody help me please? Thanks for every answer.

  • From where are you opening pdf file in webview? – Fawad Masud Jan 31 '15 at 15:05
  • I'm opening it directly from the internet, not from local directory. Can you help me please? –  Jan 31 '15 at 15:18
  • Because your using a custom UIWebView you have to create the actions it includes. For downloading it's easy. Create a directory and then save your file to it. The to open in whatever applications are installed on the device you use `UIDocumentInteractionController` There are numerous tutorials on the web. Do some research first – soulshined Jan 31 '15 at 19:35

1 Answers1

0

You can download pdf file in form of NSData and save it in document directory.

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];
Fawad Masud
  • 12,219
  • 3
  • 25
  • 34
  • Where do I have to set it. In ViewController.m? –  Jan 31 '15 at 15:30
  • Yes. Depending on your requirements, you can put this code in viewDidLoad or IBAction of a button. – Fawad Masud Jan 31 '15 at 15:32
  • I copied this code under "- (void)viewDidLoad {" in ViewController.m, right? –  Jan 31 '15 at 16:16
  • Yes right, if you want to save file as soon as the view loads. – Fawad Masud Jan 31 '15 at 17:22
  • I get two errors: NSData *dataPdf = [NSData dataWithContentsOfURL:pdfOnline.url]; Use of undeclared identifier 'pdfOnline' [[NSFileManager defaultManager] documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil]; Expected ':' What can I do to solve this error? –  Jan 31 '15 at 18:22
  • I have updated the code. In place of "YouURL " you have to put the url from which the pdf file is downloading. – Fawad Masud Jan 31 '15 at 19:26
  • @FawadMasud you should include the dispatch syntax for downloading in the background thread. – soulshined Jan 31 '15 at 19:34
  • NSData *dataPdf = [NSData dataWithContentsOfURL:deu-net.com/Berlin-Buch/14Buch.pdf]; Invalid digit 'B' in dezimal constant Use undeclared identifier 'Buch' Use undeclared identifier 'Berlin' Use undeclared identifier 'deu' Use undeclared identifier 'net' –  Jan 31 '15 at 19:40
  • Thank you very much for your answer. There is no error now. But there is still a question: How does it work now? When I open the PDF file (deu-net.com/Berlin-Buch/14Buch.pdf) in my WebView it still doesn‘t ask for "Save in…". Why that? What am I doing wrong? Some idea? –  Jan 31 '15 at 21:40
  • It will not ask automatically. You have to do it yourself. create a button and put the code inside its action. – Fawad Masud Feb 01 '15 at 00:49
  • Can I create this code on my website? Or just in my app? –  Feb 01 '15 at 11:02