2

I'd like to detect when a user presses a webform button present in an HTML page loaded in an iOS webview (ideally using WKWebView). Is this possible? And if so, how?

General comment:

I know that WKWebView has some limitations hence if you know a method that works with WKWebView please share it. If you know a method that works with UIWebView please share that as well as I can use it as start to find a solution for my WKWebView based app.

mm24
  • 9,280
  • 12
  • 75
  • 170

2 Answers2

3

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!

Jangles
  • 510
  • 6
  • 18
0

There is a workaround, demonstrated by shazron in Objective-C here https://github.com/shazron/WKWebViewFIleUrlTest to copy files into /tmp/www and load them from there.

Rahul Mayani
  • 3,761
  • 4
  • 25
  • 40
  • 1
    Thanks for the answer. The projects descriptions says "A test project to demonstrate that the WKWebView class cannot load file:// urls in iOS 8 beta 4 and 5 on a device". I don't get how I can use it to intercept the user clicks. Could you please be more specific? – mm24 Apr 09 '15 at 12:20