Capacitor: [Documentation] Document how to modify the WebViewClient

Created on 12 Apr 2020  路  5Comments  路  Source: ionic-team/capacitor

https://github.com/ionic-team/capacitor/pull/2477 was merged without any documentation on how to modify the WebViewClient, making it difficult to use without a decent amount of Java/Android development experience. I want to enable self-signed https support like in https://github.com/ionic-team/capacitor/pull/2271. The PR didn't get merged, instead I was pointed at https://github.com/ionic-team/capacitor/issues/2230, which was fixed by https://github.com/ionic-team/capacitor/pull/2477, yet no documentation was added (afaik) about how to use it. If someone could either document how to modify the webview or how I could specifically solve the https self-signed certificate problem that would be awesome.

Most helpful comment

Sorry but this is not a feature we want to "advertise", we mainly did because of you (quasar) were overriding the WebViewClient and breaking the plugins because of that.

For using it, instead of doing

public static void enable(WebView webview) {
    webview.setWebViewClient(new WebViewClient() {
      @Override
      public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
        handler.proceed();
      }
    });
  }
public static void enable(Bridge bridge) {
    bridge.getWebView().setWebViewClient(new BridgeWebViewClient(bridge) {
      @Override
      public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
        handler.proceed();
      }
    });
  }

and your EnableHttpsSelfSigned.enable(findViewById(R.id.webview)); to EnableHttpsSelfSigned.enable(this.bridge);

All 5 comments

Sorry but this is not a feature we want to "advertise", we mainly did because of you (quasar) were overriding the WebViewClient and breaking the plugins because of that.

For using it, instead of doing

public static void enable(WebView webview) {
    webview.setWebViewClient(new WebViewClient() {
      @Override
      public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
        handler.proceed();
      }
    });
  }
public static void enable(Bridge bridge) {
    bridge.getWebView().setWebViewClient(new BridgeWebViewClient(bridge) {
      @Override
      public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
        handler.proceed();
      }
    });
  }

and your EnableHttpsSelfSigned.enable(findViewById(R.id.webview)); to EnableHttpsSelfSigned.enable(this.bridge);

Thank you so much for the code snippet! It worked great, but I had to replace bridge.getWebView().setWebViewClient(new WebViewClient(bridge) { with bridge.getWebView().setWebViewClient(new WebViewClient() { (removed bridge arg).

Sorry, should be BridgeWebViewClient, not WebViewClient, updated the comment.

A big "thank you!" from Quasar team for guiding us!

@jcesarmobile
Not sure this is the right place to ask this and whether this should be an feature request instead but where is the logical place to be overriding the WebViewClient in a default template app?

In the template app for android, MainActivity calls BridgeActivity.init(), which creates a new Bridge with new default BridgeWebViewClient. new Bridge() also calls load on the WebView, with the app url from config.

If custom WebViewClient is set before init it gets overwritten. If set after init, it doesn't get set until after the initial load request.

This is fine for events triggered on response from server, ie onReceivedSslError or shouldOverrideUrlLoading but not for ones called prior to request, ie shouldInterceptRequest.

Is there a better place to put this?
Options I see to fix are:

  1. separate init and load in MainActivity, so that any custom overrides can be applied after init and prior to load (preferred option)
  2. allow Plugins to override shouldInterceptRequest as per implementation of shouldOverrideUrlLoading

My use case is loading via server url to an authenticated server, and wanting to set custom Authorization header on request to specific urls.

Was this page helpful?
0 / 5 - 0 ratings