gpt4 book ai didi

ios - wkwebview 链接在点击时不会打开

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:18 25 4
gpt4 key购买 nike

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

在 html 文件中是电话号码和网络链接。长按会打开电话号码,但 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”永远不会被调用。

最佳答案

我设法通过删除代码的“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);
}
}

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

关于ios - wkwebview 链接在点击时不会打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54335967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com