4

I know there have been couple of discussion already on this topic, but I am trying a different approach here. I am trying to first inject a variable into the Webview, then try to get the output by calling a method on that variable. Here's how it goes:

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.");
}

}

The output: version=

Community
  • 1
  • 1
rishabh
  • 1,155
  • 1
  • 10
  • 17

0 Answers0