Display PDF in UIWebView using loadData - pdf

I am trying to display a PDF I have stored locally in a UIWebView. This is how I currently attempt to do this:
if (![[NSFileManager defaultManager] fileExistsAtPath:self.url]) {
LOG_ERROR(#"Couldn't load local file. File at path: %# doesn't exist", self.url);
return;
}
nsurl=[NSURL fileURLWithPath:self.url];
NSData *data = [NSData dataWithContentsOfFile:self.url];
LOG_DEBUG(#"data length:%d",[data length]);
[self.webView loadData:data MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
I have also tried passing nil for textEncoding, as well as using UIWebView's loadRequest. The result is a UIWebView that displays a blank page. No errors occur in the UIWebView delegate method. The strange thing is that data has the correct length, in bytes, for the PDF I am trying to display, which means the file is being found and loaded correctly.
Does anyone have an idea as to what might be going wrong here or how I can better debug this problem?

Below you can find two different approaches to open a .pdf file on your UIWebView; one from a url and one as NSData:
if (self.pdfDataToLoad) //Loading the pdf as NSData
{
[self.webView loadData:self.pdfData
MIMEType:#"application/pdf"
textEncodingName:#"UTF-8"
baseURL:nil];
}
else //Open the pdf from a URL
{
NSURL *targetURL = [NSURL URLWithString:#"https://bitcoin.org/bitcoin.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.webView loadRequest:request];
}

It turns out the problem was something to do with the file format of the PDFs. I edited them using Illustrator and re-saved them. I guess Mobile Safari doesn't like the way Illustrator formatted the files because each was blank when viewed using the simulator's browser (although I could open the PDFs in regular Safari just fine).
The solution was to open the PDFs using Preview and re-save them. After running each PDF through the Preview save routine I was able to get each PDF to display in a UIWebView without changing any of my code.

You don't need NSData to load local file, loadRequest should work directly with NSURL
[self.webView loadRequest:[NSURLRequest requestWithURL:nsurl]];
I can only suggest to make NSURL like that
nsurl = [NSURL URLWithString:[self.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSData is used to load the pdf files when they are stored locally in any of the directories.
To load the pdf which was in your application, use the following code .
NSString *path = [[NSBundle mainBundle] pathForResource:#"FileNameHere"
ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

Related

UIWebView hiding when loading a full html page in, but NOT when omitting certain tags?

My UIWebView is displaying some strange behavior. When I load in a full HTML page, it just shows me a blank page. If I were, however, to load in just something like "Hi," it will display fine. my code is as follows for loading it:
NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:#"http://site.com/external/get.php?type=phone&url=%#", url]] encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:html baseURL:nil];
Assume that the html is valid, and it will always return a result if you just type in the url. That current code displays a blank UIWebView, but if I were to configure that url to just display "Hi," it would display.
You are having an encoding error. You specified, that your source would be UTF-8 encoded, which it is not.
Option 1
What is wrong with letting the UIWebView handle the request itself? It would be way easier and you don't have to mess around with encodings.
NSString *urlstring = #"http://some.url.com";
NSURL *url = [NSURL URLWithString:urlstring];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
Option 2
Try using NSASCIIStringEncoding instead of NSUTF8StringEncoding. Has worked out for a quick test for me. Other NSStringEncoding enumerations can be found here: http://api.monobjc.net/html/T_Monobjc_Foundation_NSStringEncoding.htm
For the Future:
Don't ignore the error.
NSError *error;
NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:#"http://site.com/external/get.php?type=phone&url=%#", url]] encoding:NSUTF8StringEncoding error:&error];
if (error)
NSLog(#"Error: %#", [error localizedDescription]);
In your case, this would most likely have printed out Cocoa error 261 to the console. That this means that you have a wrong encoding, can be easily found out through google and/or stackoverflow.
Also, try to debug your application, when having problems like this. The problem was the String being nil, not the WebView malfunctioning.

UIWebView local Resource main bundle html file

I've tried almost every way of loading an html file into an UIWebView that has images and resouces in the main bundle. The web page always loads but I can never get the images to show up.
NSString *html = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString *htmldata = [NSString stringWithContentsOfFile:html encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
[webView loadHTMLString:htmldata baseURL:[NSURL URLWithString:path]];
the HTML code has css:
background: url('image.png')
I've also assured that the file exists in the bundle when the app is run using other methods, but still no luck. Anyone see what's wrong with my code???
Instead of loading the HTML directly with loadHTMLString, you can have the web view do the work of loading the HTML from the bundled file. That way it should know where the HTML came from to resolve relative links.
Ex:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
I haven't tested the above, you may need the inDirectory clause of the pathForResource method. Good luck!
This is an old post, but I think a way to make this work is to change the following line:
[webView loadHTMLString:htmldata baseURL:[NSURL URLWithString:path]];
to:
[webView loadHTMLString:htmldata baseURL:[NSURL fileURLWithPath:path]];
The method in the comment and jimbojw's answer works too, but in my case htmldata is not retrieved from a file, but instead is in memory.

UIWebView not loading external images

I'm loading a UIWebView using the loadHTMLString method and depending on the connection speed, the images are sometimes not loaded and I just see a blank rectangle instead of the images. Has anyone seen this issue? Please note that the images are external links.
Maybe you need to set your baseURL. How does your HTML img sections look like? Are you referencing the entire hyperlink including the http://? Make sure the full link is in there and you should have no problem. If you are referencing the images locally in your bundle you use:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"html"];
NSString *HTMLData = [[NSString alloc] initWithContentsOfFile:htmlFile];
[webView loadHTMLString:HTMLData baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
NSURLRequest *req = [NSURLRequest requestWithURL:webUrl];
[self.WebView loadRequest:req];
if images are only external hyperlink

pages.zip file will fail to displays view using UIWebview and NSURL

I have an application that displays search tips. This will work if I store an .html file in the main bundle and display it using the Apple example code:
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
//load document --
[self loadDocument:#"searchTipsLegislators.html" inView:searchTips];
If however I change the document to one created using a compressed Pages file the app return the error:
2010-12-15 08:53:04.537 cv112[42067:7003] Cannot load iWorkImport
2010-12-15 08:53:04.538 cv112[42067:7003] Failed to generate preview
Has anyone gotten this to work?
Two things to try:
Don't compress the pages document. This is no longer necessary as of iOS 3.
Don't expect this to work on Simulator. Does it work on your device?

reading excel sheet with url in Iphone sdk

I am Having a problem here.I am downloading the excel sheet using Url and have to displaying it in the web view here my problem is the Excel sheet is not displaying.
I written the code as:
NSString *str = #"";
str = [str stringByAppendingString:
#"http://databases.about.com/library/samples/address.xls"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
[webView loadData:data MIMEType:#"application/xls" textEncodingName:#"UTF-8"
baseURL:[NSURL URLWithString:#"http://google.com"]];
[contentView addSubview:webView];
Thank you,
Monish.
Are you able to view it using Safari on the phone or does it say "unsupported format"?
Try another file that works in Safari and then try in UIWebView.
Also see this: http://developer.apple.com/iphone/library/qa/qa2008/qa1630.html
First make sure you are able to view the xls in Safari then try code like this:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://www.yourwebsite.com/good.xls"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}