wkwebview 链接在点击时不会打开

wkwebview links won't open when tapped

我正在开发一个应用程序,其中的一些代码是从另一个开发人员那里继承来的,具有加载 html 文件的 Web 视图。

在 html 文件中有 phone 号码和网络链接。如果长按 phone 个数字将打开,但 html 个链接不会打开。

我希望它们可以通过短按打开,但短按没有任何反应。如果我长按,系统对话框会弹出 "open" 选项,但按 "open" 什么都不做。

这是我刚才的代码:

#import "IntroductionViewController.h"

@interface IntroductionViewController () <WKNavigationDelegate, WKUIDelegate>

@end

@implementation IntroductionViewController

@synthesize html_file_name;
@synthesize web_view;
@synthesize spinner;

- (void)viewDidLoad {
[super viewDidLoad];

self.navigationController.navigationBarHidden = NO;

[self setTitle:_title_string];

[self.web_view bringSubviewToFront:spinner];
[spinner setHidden:NO];
[spinner startAnimating];

NSURL *url = [[NSBundle mainBundle] URLForResource:html_file_name withExtension:@"html"];
[self.web_view loadRequest:[NSURLRequest requestWithURL:url]];
self.web_view.navigationDelegate = self;
self.web_view.UIDelegate = self;
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
[spinner setHidden:YES];
[spinner stopAnimating];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
[spinner setHidden:YES];
[spinner stopAnimating];
}

- (BOOL)webView:(WKWebView *)webView shouldStartLoadWithRequest (NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"called here");
if (navigationType == UIWebViewNavigationTypeLinkClicked ) {
    UIApplication *application = [UIApplication sharedApplication];
    [application openURL:[request URL] options:@{} completionHandler:nil];
    return YES;
}
return YES;
}

@end

"shouldStartLoadWithRequest" 从未被调用。

我设法通过删除代码的 "shouldStartLoadWithRequest" 部分并添加以下部分来实现此功能:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler {
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
    if (navigationAction.request.URL) {
        NSLog(@"%@", navigationAction.request.URL.host);
        if (![navigationAction.request.URL.resourceSpecifier containsString:@"ex path"]) {
            if ([[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) {
                UIApplication *application = [UIApplication sharedApplication];
                [application openURL:navigationAction.request.URL options:@{} completionHandler:nil];
                decisionHandler(WKNavigationActionPolicyCancel);
            }
        } else {
            decisionHandler(WKNavigationActionPolicyAllow);
        }
    }
} else {
    decisionHandler(WKNavigationActionPolicyAllow);
}
}

所有链接现在都按预期打开,在单独的(新)window 网络链接中,只需轻点一下即可。

使用以下代码在短按上打开长按事件...

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
        
        switch navigationAction.navigationType {
        case .linkActivated:
            UIApplication.shared.open(navigationAction.request.url!)
            decisionHandler(.cancel)
            return
        default:
            break
        }
        decisionHandler(.allow)
    }