views:

468

answers:

2

Hi all!

I have a UIWebView that loads a local HTML file from the resources folder. Now, this HTML file has Javascript inside it.

My question is, is there a way to pass an XCode variable to the HTML file that gets loaded inside the UIWebView which I can pass to the Javascript function in the HTML.

Thanks a lot for any help or suggestion.

A: 

You can construct a Javascript string then pass into -stringByEvaluatingJavaScriptFromString:, e.g.

NSInteger u = get_integer_from_user();
NSString* script = [NSString stringWithFormat:@"call_your_function(%d);", u];
[webView stringByEvaluatingJavaScriptFromString:script];
KennyTM
KennyTM, I understood this. In my html file, I've a javascript in which there's function initialise(), which I'm calling on onload. i.e. <body onload="initialise()" onunload="GUnload()">. Now can I simply write it as initialise(number s) and procceed?
neha
@neha: Yes. But in that case you need to remove the `initialize()` in your `onload` to avoid double-initialization.
KennyTM
But, if I remove initialise() in onload(), the script doesnt find anything to execute on load and it doesn't work..
neha
@neha: You need to make a delegate to your web view and pass the script in `-webViewDidStartLoad:`. Alternatively, you can construct your HTML with `-loadHTMLString:baseURL:`.
KennyTM
Thanx for your help, KennyTM! It really helped.
neha
KennyTM, I've started implementing this part. Here, you are directly providing the webView the function name. How will it get the path of the script along with script name?
neha
@neha: Path of the script??? You control the HTML file and scripts don't you?
KennyTM
Yes. I mean, the file is in my app's resources folder. So I need to specify the path till that file somewhere in my code like [[NSBundle mainBundle] pathForResource:@"java1" ofType:@"html"] where java1.html is my html file. If I want to provide this path then I'll have to use other method than stringByEvaluatingJavaScriptFromString:
neha
@neha: But to invoke the a Javascript function you don't need to know the script's path. You just need to know the function's name. What am I missing?
KennyTM
As per my knowledge, in xcode you need to specify the script path which I mentioned, in order it to know where the script lies. Basically, I'm trying to do what you proposed i.e. to remove the onload() function, and it's just eating me out... I want to pass parameters to my script. I've tried doing it a thousand ways since yesterday. Normally I'm able to run it fine without passing parameters.
neha
A: 

You can do something like

[_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"Handler.method(%@);", variable]];

where Handler.method is a Javascript function. Make sure the page has loaded until you evaluate this Javascript, or it will not work. I use this method myself and it works, although I had to base64 encode the data to make it work. Depends on the value you store in it.

JoostK
Thanx for your help, JoostK. It helped..
neha
JoostK, here you are providing the webView the function name so where do u specify the script name and path?
neha
In my case, the `Handler.method` is just a normal Javascript function. The `UIWebView` tries to handle this JS call and looks for this method, in any `script`-element or imported Javascript. So you can just import/define you JS in the HTML file you load in the `webView`, then you can call any Javascript function from Cocoa.
JoostK
Is this true even if my javascript[which is embedded in html] is in resources folder and not written in xcode file? Because I generally provide this path while running.
neha
HTML is not something Xcode needs to compile, so it's fine within the Resources. Resources are just copied over to your application bundle, without trying to compile it. So if your HTML loads as expected, your Javascript will have been loaded as well.
JoostK