elektricni elektricni - 10 months ago 80
Objective-C Question

Disable links in WKWebView?

I want to load page in WKWebView, and then disable links in it. Most of the links are relative.

Tried with

but new page is loaded before it's called and relative links don't appear if i use
. Also tried with
, and that didn't load at all.

Any ideas? Obj C is prefferable, but Swift is also ok.


OK, I'll rephrase the question a bit.

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.

Code is simple

- (void)viewDidLoad {
[super viewDidLoad];

NSString *sURL = @"https://www.mytestingwebsite.com";

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];

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.


Since I couldn't come up with something smarter, I did it with javascript.

Basically, you load js file, look for container and links, and set href="javascript:(void)" for links you want.