UIWebView has a callback:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
This is triggered whenever a new request for a URL is collected. From javascript, you can trigger a new request URL in the onfocus event of the tag using a special scheme, for example:
window.location = "webViewCallback://somefunction";
Here's a script to post your custom event on any html page to load.
You will need to get all the HTML before loading it into a UIWebView as follows:
NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"your URL"] encoding:NSUTF8StringEncoding error:nil];
Then paste the following into the HTML text in the right place:
<script> var inputs = document.getElementsByTagName('input'); for(int i = 0; i < inputs.length; i++) { if(inputs[i].type = "text") { inputs[i].onfocus += "javascript:triggerCallback()"; } } function triggerCallback() { window.location = "webViewCallback://somefunction"; } </script>
Then, in the callback, you should do something like this:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType { if ( [[inRequest URL] scheme] == @"webViewCallback" ) {
What is it. Hope this helps.
source share