elektricni
elektricni

Reputation: 329

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 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.


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



EDIT:

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


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.

Upvotes: 5

Views: 9434

Answers (4)

Anil shukla
Anil shukla

Reputation: 277

I've come across with one solution to completely disable links including JavaScript.

I have used WKWebview in place of UIWebview to implement this.

Create one BOOL variable and set it YES in view did load.

allowLoad = YES;

Put this below code where you are loading URL to disable Java script.

WKPreferences *preferences = [[WKPreferences alloc] init];
preferences.javaScriptEnabled = NO;
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.preferences = preferences;
self.webView =[[WKWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height) configuration:configuration];
self.webView.navigationDelegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"Your URL that you want to lead"]];
[self.webView loadRequest:request];

Now add required delegate methods for WKNavigationDelegate to disable link -

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
[MBProgressHUD showHUDWithText:SyLocalizedString(@"Loading", @"L10N comment TBD") addedTo:self.view];}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[MBProgressHUD hideAllProgressHUDsForView:self.view animated:YES];
allowLoad = NO;}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if (allowLoad == YES) {
    decisionHandler(WKNavigationActionPolicyAllow);
} else {
    decisionHandler(WKNavigationActionPolicyCancel);
}}

Now after this code, you'll not be able to open any link on the page that is opened.

Upvotes: 0

pws5068
pws5068

Reputation: 2194

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
    guard navigationAction.navigationType == .other || navigationAction.navigationType == .reload  else {
        decisionHandler(.cancel)
        return
    }
    decisionHandler(.allow)
}

Upvotes: 5

abagmut
abagmut

Reputation: 911

Try:

func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void)
{
    decisionHandler(navigationResponse.response.url?.absoluteString == mainLink ? .allow : .cancel)
}

Upvotes: 0

elektricni
elektricni

Reputation: 329

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.

Upvotes: 2

Related Questions