我知道这个话题已经couple of discussion already,但我在这里尝试不同的方法。 我试图首先将一个变量注入Webview,然后尝试通过调用该变量上的方法来获取输出。这是怎么回事:
webview = [[UIWebView alloc] initWithFrame:window.bounds];
[window addSubview:webview];
[webview loadHTMLString:@"<html><head><script>(function() {var myVariable = window.myVariable = {};"
@"myVariable.getVersion = function() {return 1;}})();"
@"</script></head></html>"
baseURL:nil];
webview.delegate = self;
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if ([[webView stringByEvaluatingJavaScriptFromString:@"typeof(myVariable);"] isEqualToString:@"object"]) {
NSLog(@"version=%@",[webView stringByEvaluatingJavaScriptFromString:@"myVariable.getVersion());"]);
} else {
NSLog(@"failed the test.");
}
}
输出: 版本=