Display a .pdf in an iOS app without downloading it from the internet

风流意气都作罢 提交于 2019-12-10 22:33:08

问题


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:


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!



来源:https://stackoverflow.com/questions/33202691/display-a-pdf-in-an-ios-app-without-downloading-it-from-the-internet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!