WKWebView单击链接的位置

时间:2019-03-26 11:23:58

标签: ios ipad wkwebview

在iOS应用程序中,我使用WKWebView加载SVG图像。在此SVG内部,有链接连接到另一个对象内部的数据。我使用导航委托方法来检查是否按下了链接(例如flowchart://{id})。对于iPad设备,我希望按下链接的源矩形,以在弹出框内显示详细视图控制器。下面的代码适用于非iPad设备,但是如何获得按下的链接的矩形?

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    if ([navigationAction navigationType] == WKNavigationTypeLinkActivated) {
        if ([[[[navigationAction request] URL] scheme] isEqualToString:@"flowchart"]) {
            NSMutableArray *components = [[[[navigationAction request] URL] pathComponents] mutableCopy];

            [components removeObjectAtIndex:0]; // First element is a slash.

            NSInteger identifier = [[components firstObject] integerValue];

            Section *section = [self getSectionForID:identifier];

            SectionViewController *details = [LocalizedStoryboard(@"Main") instantiateViewControllerWithIdentifier:@"SectionViewController"];

            [details setContent:[section content]];
            [[details navigationItem] setTitle:[section title]];

            if (iPad) { // Macro to check if device is an iPad.
                CGRect rect = CGRectZero; // Get the location of the link.

                SectionNavigationViewController *popover = [LocalizedStoryboard(@"Main") instantiateViewControllerWithIdentifier:@"SectionNavigationViewController"];

                [popover setViewControllers:@[details] animated:NO];

                [popover setModalPresentationStyle:UIModalPresentationPopover];
                [popover setPreferredContentSize:CGSizeMake(400.0f, 400.0f)];

                [[popover popoverPresentationController] setDelegate:self];
                [[popover popoverPresentationController] setPermittedArrowDirections:UIPopoverArrowDirectionAny];

                [[popover popoverPresentationController] setSourceRect:rect];
                [[popover popoverPresentationController] setSourceView:webView];

                [self presentViewController:popover animated:YES completion:nil];
            } else {
                [[self navigationController] pushViewController:details animated:YES];
            }
        } else {
            [[UIApplication sharedApplication] openURL:[[navigationAction request] URL] options:@{} completionHandler:^(BOOL success) { ; }];
        }
    }

    decisionHandler([navigationAction navigationType] >= 0 ? WKNavigationActionPolicyCancel : WKNavigationActionPolicyAllow);
}

0 个答案:

没有答案