To do so using WKWebView, you need to conform to WKNavigationDelegate, and set the webView's navigationDelegate
.
The method you're probably looking for is - webView:decidePolicyForNavigationAction:decisionHandler:
, where you can determine whether to allow the link touch or not. You can get the target URL from navigationAction.request.URL
.
A quick example:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
if ([navigationAction.request.URL.relativeString hasPrefix:kLocalContentPrefix])
{
// Show some local content
// Don't let the webview load this URL.
decisionHandler(WKNavigationActionPolicyCancel);
}
else
{
// Allow the webview to load this URL.
decisionHandler(WKNavigationActionPolicyAllow);
}
}
To do this in UIWebView, it's almost the same, except you just conform to -(BOOL)webView:shouldStartLoadWithRequest:navigationType:
in UIWebViewDelegate. But I would recommend using Webkit if possible!