0

How can I save the Login information such that when I open my app (webview) I don't have to do Login?

1 Answers1

0

This following code works for me

public class WebViewActivity extends AppCompatActivity {

    private WebView webView;
    String url = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        
        webView = findViewById(R.id.webView);
        
        WebSettings webSettings = webView.getSettings();
        
        websettings.setDomStorageEnabled(true);
        websettings.setJavaScriptEnabled(true);
        webView.getSettings().setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
        webView.getSettings().setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
        
        if (Build.VERSION.SDK_INT >= 21) {
            CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
        } else {
            CookieManager.getInstance().setAcceptCookie(true);
        }
        
        webView.loadUrl(url);
        
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        }
        ...
    }
    
    ...
}

And make sure your website able to save user credentials in a cookie.

Gedanggoreng
  • 186
  • 2
  • 11
  • If my answer still can't save the password, see the answer given for my question here : https://stackoverflow.com/questions/69140584/how-to-save-username-and-password-in-webview. – Gedanggoreng Sep 26 '21 at 21:14