Search code examples
iosuiwebview

Loading Local PDF File Into WebView


I am attempting to put the following functionality into an iOS app I am writing:

  • Ship a set of PDFs in the resources folder of the project in XCode
  • Copy the PDFs to the app directory
  • Open the PDF in a webview.

As far as I can see, the first two steps work ok (I've used FileManager to check fileExistsAtPath after the copy operation). However, the webview is empty, and is erroring out ("the requested URL does not exist on server"). My code for the file open is as follows:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                          NSUserDomainMask, YES);
NSString *localDocumentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = @"example.pdf";
NSString *localDocumentsDirectoryPdfFilePath = [localDocumentsDirectory  
                                     stringByAppendingPathComponent:pdfFileName];
pdfUrl = [NSURL fileURLWithPath:localDocumentsDirectoryPdfFilePath];
[webView loadRequest:[NSURLRequestWithURL:pdfUrl];

This works fine on the simulator, but doesn't work on the device


Solution

  • As posted by Anna Karenina above, "The device is case-sensitive. Make sure the filename matches exactly"