0

My app downloads a pdf and then on a button press brings it up in a new view.

I get the error:

-[NSURL initFileURLWithPath:]: nil string parameter'

After some troubleshooting I pinned the problem to somewhere in this code snippet. The path that is being pointed to is in the /Documents folder where the downloaded pdf is placed. Thus the document is not in the main bundle.

NSString *path = [[NSBundle mainBundle] pathForResource:PDFpathwithextension ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

Here's the download code:

//Start an NSURL connection to download from the remotepath
  NSData *pdfData = [[NSData alloc] initWithContentsOfURL:remotepathURL];

//Store the Data locally as PDF File
  NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

  NSString *filePath = [resourceDocPath stringByAppendingPathComponent:[newdata.ThirdPickerName stringByAppendingFormat:@".pdf"]];
  pdfData writeToFile:filePath atomically:YES];
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Bogiematch
  • 27
  • 7

1 Answers1

0

As NSURL is telling you, you've handed it nil instead of a valid path.

nil here means no such resource could be found by that name. Indeed, your question suggests you're well aware of this.

Since you claim your app already downloads a PDF, does it actually write that out to disk? If so, you should know where the resulting file is from doing that. If not, you first need to write the actual download code!

Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
  • You should use `NSURLConnection` directly so as to avoid blocking any threads (especially the main one). Also avoids having the whole file in memory at once – Mike Abdullah Apr 09 '13 at 17:10
  • Your code for deciding where to put the file is highly fishy. Use `-[NSFileManager URLForDirectory:inDomain:appropriateForURL:create:error:]` instead – Mike Abdullah Apr 09 '13 at 17:14
  • Finally, when trying something that can fail (i.e. writing the file), *test* whether it succeeds or not! It's going to fail at some point, and you need to handle it gracefully. Most Cocoa methods these days have a variant that gives you back an `NSError` for full details of the problem. – Mike Abdullah Apr 09 '13 at 17:16
  • So I switched to NSURLConnection and got that part to work ok, switched the file placement technique and I have confirmed that the file save location is the same as the pdf viewer is looking. Now the UIwebview will load without crashing but displays nothing, it is simply blank. So on to the next problem :) Thanks, Mike. – Bogiematch Apr 10 '13 at 16:33