2

I'm looking to package a .pdf file with my app so that users can view it on one of the app's tabbed UIViews. Most of the posts I've read explain how to display a .pdf that must be downloaded first from the internet. My question is: how do you access a .pdf that is packaged with the app and display it on a UI(Web)View (with no internet access/download required)?

EDIT: This snippet was the key (where "ReadFile.pdf" was .pdf added to the project):

NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"ReadFile" ofType:@"pdf"];

rswayz
  • 1,122
  • 2
  • 10
  • 21
  • What part are you having an issue with? Getting an `NSURL` to the local PDF file or displaying the PDF once you have the `NSURL`? – rmaddy Oct 18 '15 at 21:15
  • The issue was how to access the local file (not one from the internet). Solved! – rswayz Oct 18 '15 at 21:17

1 Answers1

3

This is what you should do:

– (void) displayLocalPdfInWebView
{
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize screenSize = rect.size;

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
    webView.autoresizesSubviews = YES;
    webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

    NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"JaapSahib-Gurmukhi" ofType:@"pdf"];

    NSURL *url = [NSURL fileURLWithPath:urlAddress];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

    [webView loadRequest:urlRequest];

    [window addSubview:webView];
    [webView release];

}

Source: http://www.developerfeed.com/how-display-local-pdf-file-ios-uiwebview/

What this will essentially do is:

  1. Create a CGRect to set the screen bounds
  2. Allow autoresizing
  3. Set the UIWebView to to a resource
  4. Load the file into the UIWebView

Comment, and tell me how it goes!

Nisala
  • 1,313
  • 1
  • 16
  • 30