viper
viper

Reputation: 1906

Android webview : Show the default JS popup for web page access authentication

This is a screenshot from google chrome. When I try to access a page with basic authentication, this authentication dialog is shown. I want this feature be enabled in my android Webview as well and let the user enter their username and password. But instead it just shows the 401 unauthorized page without showing this popup. How would I achieve this in the Webview? I have tried adding these snippets but still unable to show this popup.

    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setBuiltInZoomControls(true);
    webview.getSettings().setSupportZoom(true);
    webview.getSettings().setLoadWithOverviewMode(true);
    webview.getSettings().setUseWideViewPort(true);
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webview.getSettings().setSupportMultipleWindows(true);
    webview.getSettings().setDomStorageEnabled(true);
    webview.getSettings().setDatabaseEnabled(true);
    webview.getSettings().setAppCacheEnabled(true);
    webview.getSettings().setSaveFormData(true);
    webview.getSettings().setMixedContentMode(MIXED_CONTENT_ALWAYS_ALLOW);
    webview.getSettings().setAllowFileAccess(true);
    webview.getSettings().setAllowFileAccessFromFileURLs(true);
    webview.getSettings().setAllowUniversalAccessFromFileURLs(true);
    webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

enter image description here

Upvotes: 0

Views: 974

Answers (1)

Rediska
Rediska

Reputation: 1470

You need to override WebViewClient.onReceivedHttpAuthRequest() to handle the authentication. Your code is responsible for showing the authentication dialog or retrieving saved username/password. This will not be done for you.

This is the code that worked in my app. I remove some parts of it for clarity.

    @Override
public void onReceivedHttpAuthRequest(final WebView view,
                                      final HttpAuthHandler handler,
                                      final String host,
                                      final String realm) {
    if(handler.useHttpAuthUsernamePassword()) {
        String[] creds = view.getHttpAuthUsernamePassword(host,realm);
        if(creds!=null) {
            handler.proceed(creds[0],creds[1]);
            return;
        }
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
// set up the input for user name and password here
// ... removed for clarity ...
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String name = nameEdit.getText().toString();
            String pass = passEdit.getText().toString();
            view.setHttpAuthUsernamePassword(host,realm,name,pass);
            handler.proceed(name,pass);
        }
    });
    builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
          handler.cancel();
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}

Upvotes: 1

Related Questions