wkwebview 链接在点击时不会打开 [英] wkwebview links won't open when tapped
问题描述
我正在开发一个应用程序,其中的一些代码是从另一个开发人员那里继承来的,带有一个加载 html 文件的 Web 视图.
I am working on an app, with some code inherited from another developer, with a web view that loads an html file.
在 html 文件中是电话号码和网络链接.电话号码长按会打开,但是html链接打不开.
In the html file are phone number and web links. The phone numbers will open if long pressed, but the html links are not opening.
我希望它们通过短按打开,但短按没有任何反应.如果我长按,系统对话框会弹出一个打开"选项,但按打开"什么也不做.
I would prefer them to open with a short tap, but nothing happens on short tap. If I long press then the system dialog pops up with an "open" option, but pressing "open" does nothing.
这是我刚才的代码:
#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" is never called.
推荐答案
我设法通过删除代码的shouldStartLoadWithRequest"部分并添加以下部分来使其工作:
I managed to get this working by removing the "shouldStartLoadWithRequest" section of code, and adding in the following section:
- (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);
}
}
现在所有链接都按预期打开,在一个单独的(新)窗口中显示网络链接,并且只需轻点一下即可.
All links now open as expected, in a separate (new) window for web links, and by using only a short tap.
这篇关于wkwebview 链接在点击时不会打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!