Handling certificate errors in Android Webview and clearing the certificate peferences Handling certificate errors in Android Webview and clearing the certificate peferences android android

Handling certificate errors in Android Webview and clearing the certificate peferences


Do not ever override onReceivedSslError method. Goole play will reject your upload smartest way is to handle SSL error use webSettings.setDomStorageEnabled(true);


Yes, you can use clearSslPreferences() like here:

webView.clearSslPreferences()

It'll clear your decision for this object of WebView


I will just post the answer that Tssomas has given in the original question's comments, because after all this time, it's the only solution that works reliably even though it's a hack.

Quoting Tssomas:

If the user proceeds, their preference to go ahead anyway is only kept for that session (if they close the app and start it again the dialog will re-show). So what i did to make sure the user sees the dialog every time was to add the unsecure url to an array list and add a check so that every time the webview finishes loading, the array list is checked for the webview's current url. So then of course, if the array list contains the current url, show the dialog.. its not a pretty solution at all but it works...

This is how the code might look like...

//This has to be static because it will be reset only once the app process is killedprivate static final Set<String> unsecureURLSet = new TreeSet<>();webView.setWebViewClient(new WebViewClient() {    @Override    public void onReceivedSslError(final WebView view, final SslErrorHandler handler, final SslError error) {        //Adding the insecure URL to the set        unsecureURLSet.add(error.getUrl());        //Showing a first confirmation dialog        AndroidUtils.showYesNoDialog(            //First confirmation message            "WARNING - THIS PAGE IS NOT SECURE! Are you sure you want to continue loading it?",            //First confirmation "YES" option runnable            new Runnable() {                @Override                public void run() {                    //Showing a second confirmation dialog                    AndroidUtils.showYesNoDialogWithResId(                        //Second confirmation message                        "You chose to load an unsecure page, are you sure you want to do that?",                        //Second confirmation "YES" option runnable                        new Runnable() {                            @Override                            public void run() {                                //Disregard the error and proceed with the bad certificate anyways                                handler.proceed();                            }                        },                        //Second confirmation "NO" option runnable                        new Runnable() {                            @Override                            public void run() {                                //Cancel loading the page with that certificate error                                handler.cancel();                            }                        }                    );                }            },            //First confirmation "NO" option runnable            new Runnable() {                @Override                public void run() {                    //Cancel loading the page with that certificate error                    handler.cancel();                }            });    }    @Override    public boolean shouldOverrideUrlLoading(final WebView _view, final String _url) {        if (unsecureURLSet.contains(_url)){            //Code here should mimic the dialog in onReceivedSslError            //And replace the "handler.proceed()" with a forced load of _url            return true;        }        return super.shouldOverrideUrlLoading(_view, _url);    }});