当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - 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:, 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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: