4

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

HaemEternal
  • 2,229
  • 6
  • 31
  • 50
  • 4
    The device is case-sensitive. Make sure the filename matches exactly. –  Sep 21 '11 at 15:43
  • Anna, thank you! I was about to say that I'd already checked that. But when I went back and looked again, the .pdf was infact .PDF If you add this as an answer I'll mark it as solved. Thanks again. – HaemEternal Sep 21 '11 at 16:02

3 Answers3

1

Are you sure you don't want to let the UIDocumentInteractionController do the heavy lifting for you?

UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
dc.delegate = self;
[dc presentPreviewAnimated:YES];
bshirley
  • 8,217
  • 1
  • 37
  • 43
0

As bshirley suggested UIDocumentInteractionController is a great option to present your PDF. Initially I tried using the 3rd party JSQWebViewController but I was getting a blank screen on device while on simulator it was working. UIDocumentInteractionController worked great for me! For Swift you can do:

let interactionController = UIDocumentInteractionController(url: fileURL)
interactionController.delegate = self
interactionController.presentPreview(animated: true)

and implement the delegate method:

// Mark: UIDocumentInteractionControllerDelegate
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
    return UIApplication.shared.keyWindow!.rootViewController!
}
Foti Dim
  • 1,303
  • 13
  • 19
0

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

HaemEternal
  • 2,229
  • 6
  • 31
  • 50