简体   繁体   中英

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 Javascript function in the HTML.

Thanks a lot for any help or suggestion.

You can construct a Javascript string then pass into -stringByEvaluatingJavaScriptFromString: , eg

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM