iOS get data from webview

Monkey Forums/Monkey Programming/iOS get data from webview

Leon Brown(Posted 2014) [#1]
Hi. I'm looking to have data passed from an iOS webview to Monkey using something like the following:


String ListenWebView(String Url){
	if(activeView != NULL){
		String value = [activeView stringByEvaluatingJavaScriptFromString:@..."] stringValue];
		return value;
	}
	return "";
}



This seems to crash the simulator when trying to execute the Javascript. Another idea would be to poll the webview at set intervals, but I'm not sure how this is set up - I'd assume somewhere in the @implementation part of the webview setup?


Leon Brown(Posted 2014) [#2]
I sorted this by setting up the polling in the initiation of the webview. The first thing to do was to set the @implementation webViewDidFinishLoad() as follows:

- (void)webViewDidFinishLoad:(UIWebView *)webView{

		self->finished = true;

		self->message = "";

		[NSTimer scheduledTimerWithTimeInterval:0.2
                                     target:self
                                   selector:@selector(pollCheck)
                                   userInfo:nil
                                    repeats:YES
    ];

}


This sets the webview to call the pollCheck() method every 0.2 seconds. I added the the pollCheck method to the @implementation as follows:

-(void)pollCheck
{
  self->locationURL = [webView stringByEvaluatingJavaScriptFromString:@...;"];
}


This bit executes Javascript in the browser - in this case I'm asking it to return data from instructions stored in app.data.hybrid.instruction - you can change this to execute whatever Javascript you want to use to return data.