Search code examples
iosobjective-cwkwebview

wkwebview links won't open when tapped


I am working on an app, with some code inherited from another developer, with a web view that loads an html file.

In the html file are phone number and web links. The phone numbers will open if long pressed, but the html links are not opening.

I would prefer them to open with a short tap, but nothing happens on short tap. If I long press then the system dialog pops up with an "open" option, but pressing "open" does nothing.

This is my code just now:

#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" is never called.


Solution

  • I managed to get this working by removing the "shouldStartLoadWithRequest" section of code, and adding in the following section:

    - (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);
    }
    }
    

    All links now open as expected, in a separate (new) window for web links, and by using only a short tap.