10

So I would like to detect a tap on UIWebView if it is not tapping on a link. How do I do this? I know that:

- (BOOL)webView:(UIWebView *)webview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 

is called every time a link is clicked. However how do I know if it's a tap on the UIWebView that is other than the link? I've been searching for the web and the closest I found is this. However that does not tell me whether the tap is on a link or not

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
xonegirlz
  • 8,889
  • 19
  • 69
  • 127

2 Answers2

2

Let me walk you through to why this is impossible.

- (BOOL)webView:(UIWebView *)webview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
  1. Even if it sounds ludicrous there will be calls to this delegate method when the user has clicked a link with a navigation type of UIWebViewNavigationTypeOther instead of UIWebViewNavigationTypeLinkClicked. This happens with some javascript based pages that are commonly found on the internet.

  2. Even if it sounds completely insane there will be calls to this delegate method with UIWebViewNavigationTypeLinkClicked multiple times when in fact the user hasn't clicked a link. Some websites do embed the content of an frame/iframe using a simulated click on a hidden button, which causes multiple calls with UIWebViewNavigationTypeLinkClicked.

    This strange way in embedding content with a simulated click is done to bypass the privacy settings of certain browsers. Google was sued for this some time ago and had to pay several millions penalty, because they used this trick to set cookies in the Safari browser even if the user has configured the browser to not accept these cookies.

So, if you solely rely on UIWebView's delegate method you 'll get clicks when there are none and none when there are clicks. Thus, in a uncontrolled environment this is impossible.

But, what exactly are you trying to do?

Vulkan
  • 1,004
  • 16
  • 44
0

I feel the following method should work theoretically. You could do is wrap your UIWebView in a UIView container and set your gesture recognizers on the container view. Then the touch events that are not handled by the UIWebView will be passed up the view hierarchy and be intercepted by your container view.

In the container UIView implement the appropriate handlers & you can detect taps. Hope this is clear...

If this approach is not bearing fruit, then you could go the javascript route. i.e. place javascript events as webPage loads & you are good to go. See here for more - Does UIGestureRecognizer work on a UIWebView?

Community
  • 1
  • 1
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • 3
    problem is clicking on the link still triggers the tap gesture recognizer icon – xonegirlz Mar 11 '12 at 20:36
  • worst part is - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer is called before the shouldStartLoadWithRequest is executed – xonegirlz Mar 11 '12 at 20:59
  • yes that will happen. in `gestureRecognizer` you will have to figure out some way to ignore gestures. – Srikar Appalaraju Mar 12 '12 at 04:08
  • that "some way" is actually my real question here.. I've been trying to play around with it, but got no solution – xonegirlz Mar 12 '12 at 04:41
  • actually why do you need this? couldnt you do this kinds of stuff in javascript itself considering you are using uiwebview anyway? – Srikar Appalaraju Mar 12 '12 at 04:44
  • so when I tap there is some UI that I want to show.. however I do not want to show this when the link is clicked... – xonegirlz Mar 12 '12 at 04:50
  • why cant you do it in javascript? – Srikar Appalaraju Mar 12 '12 at 05:31
  • @SrikarAppalaraju hi ! I am facing same issue. I want to handle touch in web view to make web view `endEditing = YES` so my view's accessory view display. I am able to do this with urls and other detectable items as it calls `shouldStartLoadWithRequest`. So how can I add that JS in my HTML string. I dont have url to load. Any suggestions ? – Maulik Aug 18 '18 at 15:28