Manage a webView ERR_UNKNOWN_URL_SCHEME

0

Hello, I have a problem with my application when I press the whatsapp share button, it generates the error net :: ERR_UNKNOWN_URL_SCHEME, and apply the code in my application, which is as follows:

mWebView.setWebViewClient(new WebViewClient(){

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if( url.startsWith("http:") || url.startsWith("https:") ) {
            return false;
        }

        // Otherwise allow the OS to handle things like tel, mailto, etc.
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity( intent );
        return true;
    }
});

It works fine but I want to know if: is there any way by html or javascript that this error can also be solved?

    
asked by Richard Rodriguez 14.09.2017 в 16:02
source

1 answer

3

It is assumed that what you want to load in WebView is a url, in this case the error message indicates that what you are trying to load is not valid, the schema is not an url, maybe you are trying to open other schemes or even custom schemes:

mark:
todolist:
abc:
mailito: 
telefonito:
myshcheme:
richard:

as an example if we use the telefonito: scheme, inside your page that loads in WebView ,

<a href="telefonito:123-555-5555">Call me now!</a>

When clicking, this scheme is not related to an action, you will get the error message:

  

The webpage at ............. could not be loaded because   net :: ERR_UNKNOWN_URL_SCHEME

however if you use the http: scheme that represents a url, clicking will work without problem.

 <a href="http://es.stackoverflow.com">Abrir stackoverflow!</a>

The default supported schemes are http: , https: , tel: , mailto: and even market:// in an android device to load in WebView .

What the code does within shouldOverrideUrlLoading() is only allow to load content within WebView the urls that start with http: and https: , any other scheme will handle it with a Intent , in this case I recommend you also validate that you can only perform the intent for tel: and mailto: , since in other cases it can also cause an error (for example telefonito: ):

mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if( url.startsWith("http:") || url.startsWith("https:") ) {
                    return false;
                }

               //Agregar validación para email y telefono. 
               if( url.startsWith("tel:") || url.startsWith("mailto:") ) {
                  Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                  startActivity(intent);
               }
                return true;
            }
        });
    
answered by 16.09.2017 в 00:49