I have a UIWebView loaded html string with url links inside. if I click the url links, an internal home-made browser will launch to open that link.
I added a UITapGestureRecognizer
to the UIWebView so if the user taps anywhere on the UIWebView, the UIWebView will become larger and open a fuller html string.
I set the - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
to return YES, so the UITapGesture works on the UIWebView without problem.
but a problem occurs when I click on a link inside the UIWebView. clicking on a link will invoke two operations: one is opening the browser for link and the other is to enlarge the UIWebView.
This conflicting is not what I want.
I want two cases separated, i.e., if a user click a link on the UIWebView, then browser will open for that link; if a user tap on the UIWebView where no url links exist, the UIWebView is enlarged.
How can I solve that by make two actions co-exist but no conflicting?
Thanks
I know this question is old, but I've spent a few hours on Google and Stack Overflow, and I thought it might help someone who comes across this question in the future. Hopefully this is the correct way to solve this problem.
I solved this problem by writing some JavaScript code to determine if a link was clicked. My solution was based on this answer on Stack Overflow. There's a JavaScript function that gets the list of all links in the UIWebView, and their boundaries using getBoundingClientRect(). Then the function CGRectContainsPoint() tells us if the given UITouch object is within the links bounding rectangles.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
BOOL retVal = YES;
//Check if a link was clicked
NSString *js = @"function f(){ var a = document.getElementsByTagName('a'); var retVal = new Array(); for (var idx= 0; idx < a.length; ++idx){ var r = a[idx].getBoundingClientRect(); retVal[idx] = '{{'+r.left+','+r.top+'},{'+r.width+','+r.height+'}}'; } return retVal.join(';'); } f();";
NSString *result = [self.webView stringByEvaluatingJavaScriptFromString:js];
NSArray *linkArray = [result componentsSeparatedByString:@";"];
CGPoint touchPoint = [touch locationInView:self.webView];
for ( NSString *linkRectStr in linkArray ) {
CGRect rect = CGRectFromString(linkRectStr);
if ( CGRectContainsPoint( rect, touchPoint ) ) {
NSLog(@"Link Clicked");
retVal = NO;
break;
}
}
return retVal;
}
Hopefully using JavaScript in this way doesn't cause too much of a performance hit. Especially when it has to be recalculated every tap.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With