How to load images with HTML file in UIWebView - iphone

I want to load an HTML file in an UIWebView using following lines of code:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"01Numbers" ofType:#"html"];
NSURL *url=[NSURL fileURLWithPath:htmlFile];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[WebView loadRequest:request];
I am able to load the HTML file, but this HTML file also contain some images, and that images are not loading/visible in the view, can anyone suggest any solution for this?

HTML File:
<HTML>
<HEAD>
<TITLE>My Web Page</TITLE>
</HEAD>
<BODY>
<img alt="" src="1.jpg" style="width: 100px; height: 100px;"/>
<P>This is where you will enter all the text and images you want displayed in a browser window.</P>
</BODY>
</HTML>
webView code:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"desk" ofType:#"html"];
NSURL *url=[NSURL fileURLWithPath:htmlFile];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
screenshot:

You can use following way to load HTML file,
In this way all content get converted into Data and will load in webView. (All UI related contents like images icons should be in Resources Dir of project)
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"FileName" ofType:#"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
if (htmlData)
{
[webView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:nil];
}

I assume the images are also in the bundle. You will have to make sure the path set on the tag is a reference to the location of the image file IN the bundle or wherever you are storing that.

Maybe you should try to put all you local webpage in a folder and use
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"01Numbers" ofType:#"html" inDirectory:#"Your directory"];

Related

what is the wrong when loading local resource files in webview in iphone

I am trying to load local css and html files into WebView , but the web view displays the content of css file in webview as you see in the following image ..
objective-c code:
NSString *htmlPath= [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:NO];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];
// load css styles
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"css" ofType:#"css"];
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
// load js
NSString *jsPath = [[NSBundle mainBundle] pathForResource:#"MyJS" ofType:#"js"];
NSData *jsData = [NSData dataWithContentsOfFile:jsPath];
NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];
// compose full html page
NSString *pageContent = [NSString stringWithFormat:#"%#%#%#", cssString, jsString, htmlPath];
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:#""]];
what is the problem ?
You must not use
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:#""]];
instead loadHTMLString try to use stringByEvaluatingJavaScriptFromString
And if you have any problem check the following post. I think you will get it easily
UIWebView and local css file
I have not used it for CSS but hope this will help you...Good Luck...

what is absolute url of an image for showing as webView(loadHTMLString) in objective c

i am new in objective c. i want to show an image (this image is in resources->images->a.png). Now i want to show it as loadHTMLString formate. so what will be the image path? i have used "images\a.png" as src; but does not show the image.
[webView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] baseURLWithPrefix:#"file:"]];
that should work for you, provided your htmlString contain reference to your image similar to:
<img src=\"a.png\"/>
Finally I have got the answer:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];

Load image in local HTML file -- UIWebView

I load a local HTML file in a UIWebView like so:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
In my index.html, how do I load a display a local image (added to project through Xcode) with img src="..."/> ?
Instead of loading with an NSURLRequest, read the html file into an NSString and create an NSURL to the directory with image files.
Then use UIWebView's - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL method.
So, you'd have something like...
NSURL *url = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:html baseURL:[url URLByDeletingLastPathComponent]];

Images not showing in device for Web view in iphone

I am working on one application. I stuck with one problem. I have HTML page and some images are in Resource Directory. Whenever i run the application on Simulator it works perfectly fine. But when i run the application on the device the image are not displaying but text is displaying only and instead of image Question mark is showing.
Code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"test1" ofType:#"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData: [readHandlereadDataToEndOfFile]encoding:NSUTF8StringEncoding];
NSURL *baseurl = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:htmlString baseURL:baseurl];
HTML Code:
<img src="mainGrid.png">
Change your HTML code to (file:// tells that it's local resource):
<img src='file://%#'>
Then use it in this way:
NSString *pathToLocalImage = [[NSBundle mainBundle] pathForResource:#"mainGrid" ofType:#"jpg"];
NSString *content = [NSString stringWaithFormat:htmlString, pathToLocalImage];
[self.webView content baseURL:nil];

Displaying HTML in iPhone app

How to open url in textview iphone??
Is it possible to show data with links,photos and all html entities???
You can use UIWebView to load a static contain from files; html, photo, css, javascript.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
then just add "index.html" as normal HTML standard to your project.
<html>
<head><title>Hello</title></head>
<body>
<img src="icon.png"/>
<p>Test</p>
</body>
</html>
It's not possible with UITextView control. Use UIWebView or make your own custom control.