0

I want to download a PDF file from my server and save it to the documents directory. I am able to do this using NSData but then the file in the documents directory is not a PDF: it's an NSData object. Is there any way to download and save a straight PDF file?

zoul
  • 102,279
  • 44
  • 260
  • 354
John
  • 5
  • 1
  • 3

2 Answers2

0
NSData *pdfData = [NSData dataWithContentsOfURL:
    [NSURL URLWithString:@"http://…"]];
[pdfData writeToFile:@"…" atomically:YES];

This does not work?

zoul
  • 102,279
  • 44
  • 260
  • 354
  • No. When I run this (on the simulator) and then check the documents directory the file cannot be opened in preview. I'm assuming that it is being save as an NSData object and not as a pdf file. – John Oct 08 '10 at 08:55
  • There is no “saving as NSData”. When you call `writeToFile:`, the object simply writes its contents to the specified file. Is the extension `.pdf`? Did you try to peek at the file contents? – zoul Oct 08 '10 at 09:10
  • Ok, the filename is 1.pdf. I tried to open the file with preview but it doesn't work. OSX doesn't recognise the file. – John Oct 08 '10 at 09:20
  • I just tried the code, works fine for me. Make sure that the PDF is there on the server, check the downloaded file size, check the file contents using an editor (TextMate or whatever) and post the code you’re using. – zoul Oct 08 '10 at 09:28
  • If I select the file and get info it tells me the following:kind: document – John Oct 08 '10 at 09:30
  • Sorry, I just worked it out. Even though I had the filename as 1.pdf I needed to add another pdf. NSString *filePath = [resourceDocPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf", filename]]; – John Oct 08 '10 at 09:33
0

The tricky part is to save the file regularly, so that you don't keep all data in memory (we only have 64MB available on iOS)

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
steipete
  • 7,581
  • 5
  • 47
  • 81