2
votes

Having trouble capturing touch events in UIWebView. Want to be able to sense touches on words on scrolling HTML page for the purpose of inserting hyperlinks into the HTML [which I fully control] at the point of touch. Obviously some existing agent is already sensing touches, because I get a "Copy or Define" popup with magnifying glass.

I've been able to capture touch events in a different custom UIViewController in my app that has no UIWebView, placing:

self.view.userInteractionEnabled = YES;
self.view.multipleTouchEnabled = YES;

in viewDidLoad and placing

- (BOOL) canBecomeFirstResponder
{
 NSLog(@"canBecomeFirstResponder");
 return YES;
}

in the implementation. Then "touchesBegan" is called whenever I touch anywhere on the view controller.

But the same code isn't effective on the custom view controller where most of the screen is covered by a UIWebView. Curiously, touches in the small UIToolBar at the bottom does indeed call "touches Began" but something in the UIWebView is pre-empting my "touchesBegan".

Is there a way to capture these events without disabling everything UIWebView wants to do? I wouldn't want to lose scrolling and other features.

Thanks. And Happy New Year.

1

1 Answers

2
votes

Capturing touches on a UIWebView is a Pandora's Box of pain. Since you want only taps, probably the easiest thing to do is to place a UIView with [UIColor clearColor] as the background color over top of the web view. In this view, override hitTest: (instead of overriding touchesBegan: etc.) and use it to determine the location of the tap. hitTest: returns a UIView, so if you want the web view to respond to the tap, just return the web view from your hitTest: override; otherwise return the overlay view, which will then receive all the touches... methods.