Load resources from relative path using local html in uiwebview?

This is how to load/use a local html with relative references.

Up vote 5 down vote favorite 2 share g+ share fb share tw.

I have a very simple iOS app with a uiwebview loading a very simple test page (test. Html): I load this test. Html file into my web view: NSURL *url = NSBundle mainBundle URLForResource:@"test" withExtension:@"html"; NSString *html = NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil; NSURL *baseUrl = NSURL fileURLWithPath:NSBundle mainBundle bundlePath; webView loadHTMLString:html baseURL:baseUrl; This works fine if I reference the image without the relative path and put the referenced image in the root path under Targets -> Copy Bundle Resources within XCode, however I can't get it to work with the relative path as shown in my html file.

There must be a way to do this, I have lots of images, css, javascript files that I want to load into the webview and I would like not to have to have everything in the root and have to change all the references in my web app. Ios uiwebview link|improve this question edited Jun 21 '11 at 5:54RedBlueThing12.6k52761 asked Jun 21 '11 at 5:52Matt Palmerlee380312.

This is how to load/use a local html with relative references. First while drag-drop the resource into your xcode project(consider I am dragging a folder named www from my finder window), you will get two options "create groups for any added folders" and "create folders references for any added folders". Select the "create folder references.." option.

Then use the below given code. It should work like a charm..:) NSURL *url = NSURL fileURLWithPath:NSBundle mainBundle pathForResource:@"index" ofType:@"html" inDirectory:@"www"; webview loadRequest:NSURLRequest requestWithURL:url; Now all your relative links(like img/. Gif, js/.

Js) in the html should get resolved.

Thanks for that, I'll have to try it! – Matt Palmerlee Dec 8 '11 at 20:05.

I crammed everything into one line (bad I know) and had no troubles with it: webView loadRequest:NSURLRequest requestWithURL:NSURL fileURLWithPath:NSBundle mainBundle pathForResource:@"test" ofType:@"html" isDirectory:NO.

I was using Relative to project because in the get info dialog the path it shows when I choose Relative to project looks correct. However it appears to load the image correctly when I choose relative to enclosing group. Now I've just go to get my full page to work correctly with javascript and css and all, thanks!

– Matt Palmerlee Jun 22 '11 at 15:22 Ugh! This uiwebview is so fragile, I started slowly adding more complex tests to my test page (i.e. Setting the image with css instead of inline in the img tag) and it wasn't working so I reverted back to what I had originally and it didn't work again!

Maybe I'm dealing with some sort of caching in my uiwebview? – Matt Palmerlee Jun 22 '11 at 15:36.

I've gone back and tested and re-tested this, it appears that the only way I can get it to work (since I have some files in the img folder and some in js/css, etc...) is not to use a relative path to my image in the html and have everything referenced to the bundle folder. What a shame :(.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions