There is no direct way to listen for events from UIWebView, but you can connect them. With iOS7, this is pretty easy since JavaScriptCore.framework (you need to link it to the project):
JSContext *ctx = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; ctx[@"myUpdateCallback"] = ^(JSValue *msg) { [self fieldUpdated]; }; [ctx evaluateScript:@"document.getElementById('myEditableDiv').addEventListener('input', myUpdateCallback, false);"];
(at the moment I have no way to check the code, but I hope that it works)
Before iOS7 (if you want to support a lower version, you should use this), it was a bit more complicated. You can listen to the webView delegate method webView:shouldStartLoadWithRequest:navigationType: for some custom scheme, for example:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if ([request.URL.scheme isEqualToString:@"divupdated"]) { [self fieldUpdated]; return NO; } return YES; }
and run something like this in webView:
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('myEditableDiv').addEventListener('change', function () {" @"var frame = document.createElement('iframe');" @"frame.src = 'divupdated://something';" @"document.body.appendChild(frame);" @"setTimeout(function () { document.body.removeChild(frame); }, 0);" @"}, false);"];
source share