Browsing through folders in a UIWebView - iphone

My question is the following. I'm a bit confused about the path of resources to be loaded in a UIWebView. I'ld like to make my links active. For the moment the WebView isn't getting resources (.css, .html) that are in other folders while the link between them is correct. Do you know how to fix this ?
I'm currently doing like this
NSString* path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[WebView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
Thanks for reading me !

Can you try like this, it may be useful:
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"webPage" ofType:#"html"]]]];

Related

How to use HTML file in iPhone

I want to use an HTML file in my code currently I am using following line of code:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"htm" inDirectory:nil];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[WebView loadHTMLString:htmlString baseURL:nil];
But the problem which I am facing is: the HTML Page contains some images, and all that images are in a folder in my project but I am not able to see that images in the HTML page at run time.
You can set the baseURL to your resource folder, where your images are located:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"htm" inDirectory:nil];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
NSURL *baseUrl = [[NSURL alloc]initFileURLWithPath:resourcePath];
[self.myWebView loadHTMLString:htlmString baseURL:baseUrl];
Here the possible ways to use HTML into the project,
Following code for the load HTML file into xcode project
Example 1, loading the content from a URLNSURL
NSURL *url = [NSURL URLWithString:#"http://google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
Example 2, loading the content from a string
NSString *HTMLData = #"<h1>Hello this is a test</h1>";
[webView loadHTMLString:HTMLData baseURL:nil];
Example 3, loading the content from a local file
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[webView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#""]];
Hope this code helping to develop a application

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]];

UIWebView loadHTMLString:baseURL method does not work

I want to show a local html string content in a UIWebView using this :
[webView loadHTMLString:value baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
I dont know what's going wrong, but the web view remains white
thank's for help
Try this
NSString *html = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"document" ofType:#"html"]];
[webView loadHTMLString:html baseURL:nil];