How to make Android webview remember username and password?

非 Y 不嫁゛ 提交于 2019-12-01 20:29:21

Please check whether the webview stores cookies, e.g. like this:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Go To Our Facebook Page");

    //not sure if you need this
    CookieSyncManager.createInstance(this;
    //it is default true, but hey... 
    CookieManager.getInstance().setAcceptCookie(true);

    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {

        //Do you have cookies?
        Log.d("Cookie", "url: " + url + ", cookies: " + CookieManager.getInstance().getCookie(url)); 
    }

    });
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("http://www.hksalonjob.comslogin.php");

}

EDIT: Ok, forget my lines above :-)

Be careful, what i write here is no good practice in security perception. I only show you the basics how it could basically work (totally untested).

You can exchange data between Java and Javascript with JavascriptInterface:

In Java:

 @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Go To Our Facebook Page");


    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {

        //Is the url the login-page?
        if (url.equals ("http://www.hksalonjob.comslogin.php") == true) {

             //load javascript to set the values to input fields
             SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
             String usr= prefs.getString("usr", null);
             String pwd= prefs.getString("pwd", null);
             if (usr== null || pwd == null) {
                 //we  have no values - leave input fields blank
                 return;
             }
             view.loadUrl("javascript:fillValues(" + usr + "," + pwd + ");");
        } 
    }

    });

    myWebView.addJavascriptInterface(new JavaScriptInterface(), "Android");
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("http://www.hksalonjob.comslogin.php");

}


private class JavaScriptInterface {

    /**
    * this should be triggered when user and pwd is correct, maybe after
    * successful login
    */
    public void saveValues (String usr, String pwd) {

      if (usr == null || pwd == null) {
           return;
      }

      //save the values in SharedPrefs
      SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
      editor.putString("usr", usr);
      editor.putString("pwd", pwd);
      editor.apply();
    } 
}

In Javascript:

//trigger this after successful login
function afterSuccessLogin(usr, pwd) {

    if (Android != undefined) {
        if (Android.saveValues != undefined) {

            Android.saveValues(usr, pwd);  
        }
    }       
}
// this will be triggered by webview 
function fillValues(usr, pwd) {

    //fill input fields with values       
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!