Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPad UIWebView position a UIPopoverController view at a href link coords

I'm hoping that this question isn't a stupid one but how does one go about positioning a UIPopoverController view over a UIWebView so that the popup view arrow points at the UIWebView link that was clicked to show it?

I'm using the delegate method;

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( [[[inRequest URL] absoluteString] hasPrefix:@"myscheme:"] ) {
//UIPopoverController stuff here
return NO;
}

}

to capture and route the click but I'm unsure how to get the link coords to position the popup view.

Any help or pointer to relevant info would be very much appreciated.

like image 352
andrewdotcom Avatar asked Dec 28 '22 12:12

andrewdotcom


1 Answers

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 method hitTest. After that, you need to set a delegate for your webview and implement the method -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 ?

like image 57
CedricSoubrie Avatar answered Jan 30 '23 00:01

CedricSoubrie