1
\$\begingroup\$

I have a WebView, which loads the returned from a request url. The thing is, the url might be a scheme to my app, and if that's the case I need to handle it properly, rather than open it in a WebView. I am not sure if this is the best way to implement that check:

In WebViewClient:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

        String appScheme = getResources().getString(R.string.app_scheme);
        if (url.startsWith("http://" + appScheme + "//checkout")) {
            url = url.substring(7);
            url = new StringBuilder(url).insert(appScheme.length(), ":").toString();
        } else if (url.startsWith("https://" + appScheme + "//checkout")) {
            url = url.substring(8);
            url = new StringBuilder(url).insert(appScheme.length(), ":").toString();
        }

        if (url.startsWith("http:") || url.startsWith("https:")) {
            return super.shouldOverrideUrlLoading(view, url);
        }

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

I should also note, that the returned scheme sometimes looks like this:

http://myAppScheme//checkout

when my app can read only this template:

myAppScheme://checkout

I have no control over how the scheme is returned, because this happens from the payment provider I am working with. This is why I need to convert it in the proper template when it's returned in a wrong one.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.