禁用 WKWebView 中的链接? [英] Disable links in WKWebView?
问题描述
我想在 WKWebView 中加载页面,然后禁用其中的链接.大多数链接都是相对的.
I want to load page in WKWebView, and then disable links in it. Most of the links are relative.
尝试使用 webView:decidePolicyForNavigationAction:decisionHandler:
但在调用之前加载新页面,如果我使用 navigationAction.request.URL
,则不会出现相关链接.还尝试了 UIDelegate
,但根本没有加载.
Tried with webView:decidePolicyForNavigationAction:decisionHandler:
but new page is loaded before it's called and relative links don't appear if i use navigationAction.request.URL
. Also tried with UIDelegate
, and that didn't load at all.
有任何想法吗?Obj C 是首选,但 Swift 也可以.
Any ideas? Obj C is prefferable, but Swift is also ok.
好的,我将问题重新表述一下.
OK, I'll rephrase the question a bit.
当用户按下链接时,我想停止 WKWebView 来打开它,而是将该链接保存到数组.如果满足某些条件,也许可以使用 loadrequest 手动打开它.
When user presses link I want to stop WKWebView to open it, and instead save that link to array. Maybe open it manually with loadrequest if certain conditions are met.
代码很简单
- (void)viewDidLoad {
[super viewDidLoad];
NSString *sURL = @"https://www.mytestingwebsite.com";
NSURL *URL = [NSURL URLWithString:sURL];
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
[self addUserScriptsToUserContentController:theConfiguration.userContentController];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
webView.navigationDelegate = self;
webView.UIDelegate = self;
//webView.allowsLinkPreview = TRUE;
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:URL];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];
}
他们在 WWDC 2014 中使用了视频 206 NSURL *selectedURL = navigationAction.request.URL;
来检测和禁用外部链接.但这显然对我不起作用.我刚刚添加了 NSLog 和断点来测试它.
They've used in WWDC 2014, video 206 NSURL *selectedURL = navigationAction.request.URL;
to detect and disable external links. But that obviously isn't working for me. I've just added NSLog and breakpoint to test it.
推荐答案
由于我想不出更聪明的方法,所以我用 javascript 做到了.
Since I couldn't come up with something smarter, I did it with javascript.
基本上,您加载 js 文件,查找容器和链接,并为您想要的链接设置 href="javascript:(void)"
.
Basically, you load js file, look for container and links, and set href="javascript:(void)"
for links you want.
这篇关于禁用 WKWebView 中的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!