Loading external image in local HTML inside UIWebView - html

I have an NSString containing HTML string with external image URL. For example,
NSString *HTMLString = #"<p><img src="http://www.stackoverflow.com/image.jpg" /></p>";
When I use UIWebView to load HTML and present it in my app, the app freeze until the UIWebView done loading the web view.
Is that an issue about the download of image is not asynchronous? I try to find related topic but I can't get the answer.
How to make my image to be downloaded asynchronously?
Background: The HTML string is already loaded asynchronously using AFNetworking. All the text can be shown without loading the image. However, the process of downloading image freezes my app.

I don't really understand what you are trying to do, but if you want to show an image from a URL you can do that easily with a UIImageView like this:
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:#"http://www.stackoverflow.com/image.jpg"]]];
Or if you want a UIWebView you could do a separate HTML file and load it like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webView setScalesPageToFit:YES];
Or you can test to add tags like html, body etc.
I'm open for more info about your problem.

Related

WebView not loading an HTML file properly

I have a UIWebView in which I want to load an HTML file from an URL. This is the code I use to load the file with WebView :
NSURLRequest* blogRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.url]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:5.0];
[self.webView setDelegate:self];
[self.webView loadRequest:blogRequest];
The file I want to load (let's say http://example.com/foo.html) is nothing special, just plain text with basic HTML tag in it. The WebView is working properly for normal website, like google.com, but not my custom HTML file. All I get is an empty document.
webViewDidFinishLoad is never reach.
If I load my HTML file locally from my project, there is no problem. So, why isn't it working when downloading from internet? I have absolutely no clue.
Do you have a super slow network and your 5 second timeout is expiring?
Or, are you implementing a delegate method that is blocking the request?
Try this, it works for me:
NSString *URLString = #"http://example.com/foo.html";
NSURL *URL = [NSURL URLWithString:URLString];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:request];

iOS7: Display an Image in an HTML File loaded into a UIWebView

I am programming in iOS7. In my app I am using a UIWebView to load static HTML files that are packaged with the app. I want to show images, also packaged with the app, in my HTML files, but can not find the magic combination to get the images to show when the HTML file is loaded.
I saw some information on base64, which I did not understand, but I would like to find a simple solution because there will be others beside myself writing most of the HTML files.
Below is the basic format of the HTML code I am using. I have tried all manner of “/“ modifiers to no avail, including the full path as listed in the File Inspector.
<img src=“data:/Supporting Files/Info/InfoRecycle.png">
Does anyone know how I get the image to show within the HTML file?
Try putting the image in the same folder as your html file. Then reference it like this:
<img src="InfoRecycle.png">
See if that works.
I finally got it to work! I found the key to the answer in this post. I guess I did not find it earlier because I was focused on iOS7:
Using HTML and Local Images Within UIWebView
I had to make some modifications in my Xcode, so it now looks like this:
//..Loads a Local HTML File
strHTML = #"Info";
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:strHTML ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[_infoWebView loadHTMLString:htmlString baseURL:baseURL];
My reference to the image in the HTML code is now as suggested above by Stepan Grigoryan.
<img src="InfoRecycle.png">

How can i load an HTML code in a UIWebView?

My data is coming from the servers and I am storing it in a dictionary. There is a key called description in the dictionary which contains the HTML code for the description. I don't want to save the HTML code in a file because the dictionary contains a lot more dictionaries with similar description key within each. I want to load the HTML file on the UIWebView directly from the code.
Please Help.
Thanks in advance.
For example:
NSString *htmlString = [result objectforKey:#"HtmlValue"];
[webview loadHTMLString:htmlString baseURL:Nil];
[webview loadHTMLString:stringName baseURL:Nil];
Also Visit here it will be more helpful for you
Add file.html in your project resources.
Use this code to load in the web view:
NSString *path = [[NSBundle mainBundle]pathForResource:#"file" ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[_helpWebView loadHTMLString:htmlString baseURL:nil];

UIWebView load html file from bundle, and provide file with information?

I'm interested in replicating Mobile Safari's "Cannot Open Page" StandardError page.
In order to do that, I've been researching how mobile safari handle's an error when loading a page and opens the html file located in the application bundle.
In that bundle, the file: StandardError.html is loaded and produces an error while showing the mobile safari icon. The one thing that I'm having trouble with is understanding how the error is passed from the webview to the html file.
In the body of the html file, there is a variable with %#. I'm assuming that the %# indicates where Apple provides the error that caused the webview to fail loading. What I want to know is how to provide the title and error to an html file, similar to the StandardError.html file found in Mobile Safari's application bundle.
Any help would be greatly appreciated!
A UIWebView can load HTML from a string so you can do this:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"StandardError" ofType:#"html"];
NSString* errorHTML = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *htmlString = [NSString stringWithFormat:errorHTML, weberror.localizedDescription];
[self.webView loadHTMLString:htmlString baseURL:nil];
Or something like that.

Adding an image in an html file with iOS

In my app I'm presenting a report of the app data. The report is a "self-generated" html file presented on an UIWebView.
I need to include in the report (in the html file) an image that is stored in the device. At this moment I'm able to get the path of the image. It's something like "/var/mobile/Applications/A10781A1-DE2B-4651-ADFB-7A6AD9B3645A/Documents/EE20AF92-215E-4DF5-8E33-0713557A34C9"
How can I include the image in the html file?
You can include the image using:
<img src="img.png">
and
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
You can copy your image img.png to documents directory for example and set the baseURL to be the Documents directory.
Update:
I found the source where I found out how to do it some time back. Maybe you can dig from there some more useful info:
http://iphoneincubator.com/blog/windows-views/uiwebview-revisited
Its not clear if your building the html on the fly from your question. But you can do something like this.
NSMutableString *htmlPage = [[[NSMutableString alloc] initWithCapacity:1000] autorelease];
... // Build the page string
[htmlPage appendStringWithFormat:#"<img src=\"%#\" alt=\"image\" height=\"100\" width=\"100\"/>",[[NSBundle mainBundle] pathForResource:#"myImage" ofType:#"png"];
Or some variant of this.