5

I'm trying to open up a Chrome custom tab from within a webview inside of an Android app. In order to do this, I figured I could register a custom URL handler(customtab://www.myurl.com/). The error I'm running into is that chromium(the webview) is blocking the custom URL handler due to an insecure content error (This request has been blocked; the content must be served over HTTPS.). The URL is https. Even adding webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); on the webView didn't work.

Any ideas on how I can register this custom handler as "secure"?

klinquist
  • 246
  • 1
  • 4
  • 15

4 Answers4

0

You can use web intent to open url in chrome

Intent webIntent = new Intent(Intent.ACTION_VIEW);

webIntent.setData(Uri.parse(url));

getActivity().startActivity(webIntent);

Community
  • 1
  • 1
Uma Achanta
  • 3,669
  • 4
  • 22
  • 49
0

You need to override this method on the Webview to skip ssl errors

public synchronized void onReceivedSslError(WebView view, final SslErrorHandler handler,SslError error) {
        handler.proceed();                      
      }
Vardaan Sharma
  • 1,125
  • 1
  • 10
  • 21
  • webview doesn't have an onReceivedSslError method to override, WebViewClient does. When I override WebViewClient's onReceivedSslError it does not get called. `[chromium] [WARNING:web_contents_impl.cc(3535)] XXXXXX ran insecure content from customURLHandler://YYYY [chromium] [INFO:CONSOLE(0)] "Mixed Content: The page at 'XXXX' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'customURLHandler://YYYY'. This content should also be served over HTTPS.", source: XXXXX (0)` – klinquist Mar 28 '17 at 19:47
  • How does this answer the question? What TLS error are you trying to intercept? – rds Apr 03 '17 at 21:56
0

I am open my custom URL in web view in android with follow below process.

Design your webview as full screen like

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="20dp"
    android:gravity="center"
    android:background="#ffffff">

    <WebView
        android:id="@+id/WebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"/>

</RelativeLayout>

And in you .java file add below code in onCreate method

webView = (WebView) findViewById(R.id.WebView);
        windowwidth = getWindowManager().getDefaultDisplay().getWidth();
        windowheight = getWindowManager().getDefaultDisplay().getHeight();

        getWindow().setLayout(windowwidth, windowheight);

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAppCacheEnabled(false);
        settings.setDomStorageEnabled(true);





        try {
// here i call progressdialog which is load still the page is not load //properly
            Constants.ShowProgressDialog(MyActivity.this, "", "Please wait...");


            webView.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    Log.i("", "Processing webview url click...");
                    view.loadUrl(url);
                    return true;
                }

                public void onPageFinished(WebView view, String url) {
                    Log.i("", "Finished loading URL: " + url);
// dismiss the progressbar after load the web page in webview.
                    Constants.disMisProgressdialog();
                }


                @Override
                 public void onLoadResource(WebView view, String url) {
                    super.onLoadResource(view, url);
                }
            });

                webView.loadUrl("<your url>");

        } catch (Exception e) {
            e.printStackTrace();
        }

If still any issue please provide me your url I'll check and provide you the code.

Hope your problme resolve.

Make sure provide permission in Manifest file.

"<"uses-permission android:name="android.permission.INTERNET" />" (remove the quatation mark)

Arpan24x7
  • 648
  • 5
  • 24
  • How does it answer the question? – rds Apr 03 '17 at 21:47
  • How does it answer the question? --- bro as per user ask the question that he/she having issue regarding url of http and https so i provide the solution that here he/ she can load any url in internal android browser. have you any reegarding this than feel free to ask in place of down point :-| – Arpan24x7 Apr 07 '17 at 05:29
  • What part prevents "This request has been blocked; the content must be served over HTTPS." error when user clicks on `customtab://www.myurl.com/`? – rds Apr 07 '17 at 17:22
0

You simply need to register a WebViewClient in your WebView and override the shouldOverrideUrlLoading() method to support your own scheme:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
  if (url.startsWith("custometab://")) {
    // TODO: start custom tabs for the url.
    return true;
  } else {
    return false;
  }
}

(see https://stackoverflow.com/a/5514668/94363)

The problem is: how do you know when clicking on customtab://www.myurl.example.com/ if you should open http://www.myurl.example.com or https://www.myurl.example.com?

  • You could either introduce customtab:// and customtabs://
  • or do some manipulation of the URL customtab://http/www.myurl.example.com

But in fact, I don't think we need a custom scheme at all if you want to open all links with custom tabs. Just use:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
  // TODO: start custom tabs for the url
  return true;
}
Community
  • 1
  • 1
rds
  • 26,253
  • 19
  • 107
  • 134
  • See my reply to Vardaan Sharma below - the webview client isn't even receiving the custom URL. It seems that chromium is blocking the URL from being sent to the webview client. – klinquist Apr 04 '17 at 00:32
  • I was suggesting to _not_ have a custom scheme. So what custom scheme is blocked? – rds Apr 04 '17 at 16:46
  • And I think my answer is correct, except if your web server defines a Content-Security-Policy which says to not load anything but 'https://". Check your HTTP config. – rds Apr 04 '17 at 16:55