问题
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:
- Create a CGRect to set the screen bounds
- Allow autoresizing
- Set the UIWebView to to a resource
- 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