Display a .pdf in an iOS app without downloading i

2019-07-08 07:53发布

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"];

1条回答
Anthone
2楼-- · 2019-07-08 08:40

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!

查看更多
登录 后发表回答