tags:

views:

23

answers:

1

I am having difficulty with a UIWebView which shows http URLs correctly, but not bundled .pdf files.

I have also read alternative solutions such as using QuartzCore framework, however this does not seem to support multiple pages in PDFs.

Here is the code that works (for google dot com)

- (void)viewDidLoad

{
 [super viewDidLoad];

 NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@".pdf"];

 NSURL *pdfURL = [NSURL URLWithString:pdfPath];

 CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
 webFrame.origin.y += kTopMargin - 20.0; // leave from the URL input field and its label
 webFrame.size.height -= 00.0;
 self.webView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];
 self.webView.backgroundColor = [UIColor whiteColor];
 self.webView.scalesPageToFit = YES;
 self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
 self.webView.delegate = self;
 [self.view addSubview: self.webView];

 [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
}

And for the PDF, I have replaced

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];

with

[self.webView loadRequest:[NSURLRequest requestWithURL:pdfURL]];

My immediate thought was that the iPhone SDK does not support PDF viewing in its webview, however, tutorials like this one seem to show other users having success with this. So do you think I am just missing something in my code?

Thanks.

+3  A: 

UIWebView does support pdf when using loadRequest as you are doing.

pathForResource:@"file" ofType:@".pdf"

Take the . out of .pdf

Edit:

[NSURL URLWithString:pdfPath];

Use fileURLWithPath: instead.

Check that pdfPath and pdfURL are what you expect them to be.

NSLog( @"%@" , pdfURL );
drawnonward
Fantastic, those two changes were exactly what it needed. Thank you!
ajkochanowicz
@ajkochanowicz - Then please mark this answer as the correct one
willcodejavaforfood