How to get hyperlink coordinates in UIWebview

I am loading a PDF document (having multiple hyperlinks) in UIWebview. I have to show UIPopoverhyperlink dynamically.

I can fix the coordinates of the hyperlink using the TapGesture Action method

- (void)tapAction:(UITapGestureRecognizer *)sender    
{
    self.point = [sender locationInView:self.myWebView];  
}

And UIPopoverhyperlink view using below method

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *rqstUrl = [request URL];
    if (([[rqstUrl scheme] isEqualToString: @"https"] || [[rqstUrl scheme] isEqualToString: @"http"]) && (navigationType == UIWebViewNavigationTypeLinkClicked))
    {
        [self.myWebView stopLoading];
        CGRect rect = CGRectMake(self.point.x,self.point.y-5, 5, 5);
        UIPopoverController *popController = [[UIPopoverController alloc] initWithContentViewController:contentViewController];
        popController.popoverContentSize = CGSizeMake(500, 200);
        self.popController = popController;
        self.popController.delegate =self;
        UIPopoverArrowDirection direction = UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown;
        self.popController.popoverLayoutMargins = UIEdgeInsetsMake(0, rect.origin.x, 1, 1);
        [self.popController presentPopoverFromRect:rect inView:webView permittedArrowDirections:direction animated:YES];
    }
    return YES;
}

But the problem is that I knocked on two different locations for 1 or 2 seconds, for example, First Tap - In the hyperlink , and the second press is in a different location in UIWebview" UIPopover- presenting in the second contact point only not in the hyperlink location. I have to show UIPopoveronly in the position of the hyperlink, not in another place. How can I solve this problem?

+4
1

  • , . UITapGestureRecognizer :

    • , , UITapGestureRecognizer.
    • , UIWebview Hyperlink , centroid. , popover .
  • UIPopoverController iOS 9.

    "UIPopoverController . UIViewController. UIModalPresentationPopover UIPopoverPresentationController.

  • tapAction shouldStartLoadWithRequest . , .

enter image description here

, , , . - , . , .

class TapOverlayView: UIView {
    var centroid:CGRect = CGRect.zero

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        centroid = CGRect(origin: point, size: CGSize(width: 1, height: 1))
        return nil // tap through
    }
}

extension ViewController: UIWebViewDelegate {
    public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        let rqstUrl = request.url

        if( rqstUrl!.scheme?.contains("http"))! && ( .linkClicked == navigationType) {
            webView.stopLoading()

            let contentViewController = storyboard!.instantiateViewController(withIdentifier: "popover")
            let popController = UIPopoverController(contentViewController: contentViewController)
            contentViewController.modalPresentationStyle = .popover
            popController.contentSize = CGSize(width: 200, height: 40)
            let direction:UIPopoverArrowDirection = .down
            popController.layoutMargins = UIEdgeInsetsMake(0, tap.centroid.origin.x, 1, 1)
            popController.present(from: tap.centroid,
                                  in: webView,
                                  permittedArrowDirections: direction,
                                  animated: true)
        }
        return true
    }
}

GitHub .

+3

Source: https://habr.com/ru/post/1608152/


All Articles