I’m displaying HTML content using a UIWebView and would like some action to occur whenever the user clicks anywhere that isn’t a link.
I added a UITapGestureRecognizer, however at the point handleTapGesture is called I do not yet know if the user has clicked on a linked or non linked part of the screen.
To work around this within handleTapGesture I added performSelector:@selector(performAction) withObject:self afterDelay:n]
Then if shouldStartLoadWithRequest is subsequently called I set a flag that performAction checks to see if it actually should perform the action or not.
This works perfectly – but only if the time delay n is sufficiently long enough (about 0.3 to 0.4 seconds) which is too long from a users perspective as its a noticable delay. Also I guess the value of n would likely need to vary on different devices with different processor speeds and so its a fragile solution.
Is there an elegant solution?
Do you have control over the HTML? If so, you can add a global click detection, e.g.
<body ontouchdown='detectTouch()'>and then use a custom message to your ObjC app, e.g.location.href="touch://whatever". Watch for the link/location change messages in your UIWebView (shouldStartLoadWithRequest) and parse the scheme and data passed. Don’t propagate on link clicks. Should work good.If you don’t have control over the HTML, you can always inject your touch handler, but it’s trickier as you’ll need to figure out the order of events during a link click, and ignore the ones that aren’t links.