我正在开发一个应用程序,该应用程序的代码继承自另一个开发人员,并具有可加载html文件的网络视图。
在html文件中是电话号码和Web链接。如果长按,则电话号码将打开,但是html链接未打开。
我希望它们能短按一下打开,但是短按不会发生任何事情。如果长按,则系统对话框会弹出“打开”选项,但按下“打开”则无济于事。
这是我的代码:
#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”。
答案 0 :(得分:1)
我设法通过删除代码的“ 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);
}
}
现在,所有链接都将按预期方式打开,在单独的(新)窗口中显示Web链接,并且只需短按即可。
答案 1 :(得分:0)
短按下面的代码打开长点击事件...
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
switch navigationAction.navigationType {
case .linkActivated:
UIApplication.shared.openURL(navigationAction.request.url!)
decisionHandler(.cancel)
return
default:
break
}
decisionHandler(.allow)
}