How to pass an XCode variable to the HTML file in resources folder?
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 Ja开发者_C百科vascript function in the HTML.
Thanks a lot for any help or suggestion.
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];
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.
精彩评论