0

This code fragment:

NSString *content = @"<html><head></head><body bgcolor=#AAAAAA></body></html>";
[wv loadHTMLString:content baseURL:[NSURL URLWtihString:@""];

does not work with any other baseURL except for one written above (even with nil). This behaviour prevents me from using local data in my webViews. Can anyone be any assistance with this? In case i set any other baseURL, content is just not being loaded

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Concuror
  • 408
  • 3
  • 9
  • could you show any other url that you are trying with? – sergio Aug 16 '11 at 13:59
  • I've tryed `nil` `NSString *path = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent]; NSURL *baseURL = [NSURL fileURLWithPath:path isDirectory:YES];` for example. Well and many more from different examples i've found on the internet – Concuror Aug 16 '11 at 14:11

1 Answers1

2

Apart from suggesting what I am using in my code:

[webView loadHTMLString:[....
               baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

I cannot do much. Using [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] is not correct, because that way you are moving upwards and outside the app bundle, but since you say you are trying many variants, I am not sure what could be.

If you want to try and troubleshoot what happens with your baseURL, override webView:shouldStartLoadWithRequest: and have a look at what files your web view attempts to open.

EDIT:

if you need accessing the Library folder of your app, this is the way to go (source)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
[webView loadHTMLString:[....
               baseURL:[NSURL fileURLWithPath:libraryDirectory]];

Anyway, from your comment: "they were previously downloaded by my app", you should already know how to go there... anyway, I suggest again to check what files are you trying to access in webView:shouldStartLoadWithRequest:...

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • I like the hack with `webView:shouldStartLoadWithRequest:`! – Anton Aug 16 '11 at 14:50
  • I was trying to move one level up from bundle, because my local files are in Library folder rather than resource since they were previously downloaded by my app. But i tried `[[NSBundle mainBundle]` bundlePath] as well. And just to make list complete @"http://www.google.com" as well. No luck so far. – Concuror Aug 16 '11 at 14:56
  • To my pity, it doesn't go to `webView:shouldStartLoadWithRequest:` but seems to land directly in `webViewDidFinishLoad:` – Concuror Aug 16 '11 at 15:02
  • You cannot move outside of the bundle (sandbox) under iOS; see my edit about the library folder... the HTML you posted above, should not need any external file, actually... – sergio Aug 16 '11 at 15:02
  • Well yeah, but the one i'm actually generating is big and complex, whereas this fragment does not work as well. Thanks for library folder edit, but generally i just want to solve the issue with baseurl. I thought that simple HTML I've posted in question should load for any URL, but it does not. – Concuror Aug 16 '11 at 15:08
  • Indeed, it should work even with baseURL set to nil (and I know for sure that it works)... and if `webViewDidFinishLoad`, then this means that everything was ok... could you try something like: `TEST HTML`? – sergio Aug 16 '11 at 15:44
  • I will as soon as I get back to work. Spent half of the day solving this mysterie... Must have crap-coded somewhere else :( – Concuror Aug 16 '11 at 18:38
  • Ok, I've solved the issue. I've really crap coded elsewhere. Thank you for your help. – Concuror Aug 17 '11 at 07:10