4

我知道已经有几个关于这个话题的讨论,但我在这里尝试不同的方法。我试图首先将一个变量注入 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.");
}

}

输出:版本=

4

0 回答 0