I have a solution that works but I think there is a better way to do it.
I created a TouchableWebView that inherits from UIWebView and the save the touch position in the function hitTest. After that, you need to set a delegate for your webview and implement the function – webView:shouldStartLoadWithRequest:navigationType:
TouchableWebView.h :
@interface TouchableWebView : UIWebView {
CGPoint lastTouchPosition;
}
@property (nonatomic, assign) CGPoint lastTouchPosition;
TouchableWebView.m :
// I need to retrieve the touch position in order to position the popover
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
self.lastTouchPosition = point;
return [super hitTest:point withEvent:event];
}
In RootViewController.m :
[self.touchableWebView setDelegate:self];
// WebView delegate, when hyperlink clicked
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
CGPoint touchPosition = [self.view convertPoint:self.touchableWebView.lastTouchPosition fromView:self.touchableWebView];
[self displayPopOver:request atPosition:touchPosition isOnCelebrityFrame:FALSE];
return FALSE;
}
return TRUE;
}
It's not good because documentation of UIWebView says that you shouldn't subclass it. I guess there is a nicer way but this does work. Did you find another solution ?