Reputation: 484
I have a well formed html file, let's call it index.html. It is in the app bundle. This file contains links to css style sheets and local javascript files. If I use
[self.ourWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
all of the javascript/css items in the file work just fine.
If however I read the contents of the file into a string(and update the string to escape the " and / items) then use
[ourWebView loadHTMLString:HTMLString baseURL:nil];
The html markup gets rendered, but none of the javascript/css gets honored or executed.
I even tried reading from the known good file, rewriting to another file in the Documents directory, and then using loadRequest to load it. That does not execute the javascript either.
Am I missing something obvious?
Thanks, Ken
Upvotes: 1
Views: 3020
Reputation: 28572
(Mashup of my comments above)
You claim that:
The html markup gets rendered, but none of the javascript/css gets honored or executed.
using the following code:
[ourWebView loadHTMLString:HTMLString baseURL:nil];
You need to pass the appropriate value to the baseURL
parameter instead of nil
. This value depends on where your javascript/css files are within the bundle.
Later, in the comments, you say that:
I have created a directory in the documents directory called myDir which contains all the javascript files.
And you have modified the code to the following (taken from your comments):
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
filePath=[documentsDirectory stringByAppendingFormat:@"myDir"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
[self.ourWebView loadHTMLString:thisString baseURL:fileURL];
The way you are creating the path does not seem right. NSString
has a number of methods to work with paths. Use them.
Replace:
filePath=[documentsDirectory stringByAppendingFormat:@"myDir"];
with this:
filePath=[documentsDirectory stringByAppendingPathComponent:@"myDir"];
Upvotes: 1