The code you posted:
[webView stringByEvaluatingJavaScriptFromString:@"test();"];
is correct. The only thing I can think of as cause of your problem is that for some reason the javascript file containing the test function is not being loaded.
Try this:
NSString* result = [webView stringByEvaluatingJavaScriptFromString:@"document.title;"];
and see whether you get a value in result.
EDIT:
If you are loading a local HTML file which contains references to local javascript files, then the most likely cause for your kind of wrong behavior, is not specifying the baseURL when initializing the UIWebView, e.g.:
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
This should allow you to correctly load javascript files available in your resources folder:
NSString* basePath = [[NSBundle mainBundle] bundlePath];
[_label loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:basePath]];
If you give more details about how your HTML/javascript files are organized, I can help you further with this (if my hypothesis is correct).
EDIT 2:
(In the following, I assume that your are on MacOS, but I realize that this has not been stated anywhere here; if it where iOS, similar reasoning would apply, but details would be different)
First off, does your delegate method webView:didFinishLoadForFrame: get called? Or is –webView:didFailLoadWithError:forFrame: called?
As stated in another answer, you can only execute javascript after the webView has been loaded.
On the other hand, I am pretty sure that you are not passing correctly the baseURL
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"allFiles"];
htmlData = [NSData dataWithContentsOfFile:htmlPath];
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:htmlPath]];
in this snippet, baseURL is your <prefix>/allFiles/index.html file; you should simply pass <prefix>/allFiles for baseURL, and if the rest is ok (meaning, the didFinishLoad thing, and the html file referring the scripts with a relative path to the script directory) it should work then.
Now, I cannot suggest a way to find out properly the path to the directory "allFiles" (pretty late here), but just to make a test you could trim your current htmlPath to remove from it the "index.html" substring...