views:

42

answers:

1

Problem: I have a webview that is loading a page from Http://somerandomsite-here before the page is loaded I would like to add in the <head> block a reference to a .js that is in my bundle.

How do I do that?

A: 

Found the answer by myself:

NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    [sourceWeb stringByEvaluatingJavaScriptFromString:jsCode];

in test.js place this code in a function that you'll call (see below)

Function setup() { var fileref=document.createElement('script')
        fileref.setAttribute("type","text/javascript")
        fileref.setAttribute("src", "filename.js");}

when you think it's right do it for you particular case, call this:

NSString *setup = [NSString stringWithFormat:@"Setup()"];
[sourceWeb stringByEvaluatingJavaScriptFromString:setup];
amok
keep in my that this approach doesn't work if you use callbacks in your JS. If anyone knows please fire away!
amok