Make Android Webview Not Store Cookies or Passwords

Make Android WebView not store cookies or passwords

You can use this to prevent cookies from being stored and clean cookies already stored:

CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookies(callback);
cookieManager.setAcceptCookie(false);

WebView webview = new WebView(this);
WebSettings ws = webview.getSettings();
ws.setSaveFormData(false);
ws.setSavePassword(false); // Not needed for API level 18 or greater (deprecated)

how to prevent Webview from using cookies in android

Remove the old cookie data

CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.setAcceptCookie(false)

And try to handle override of webview url

mWebView.setWebViewClient(new WebViewClient() {  
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{

return false;
}
});

Cookies are not being stored in Android Webview

You could use window.localStorage instead of cookies. https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

WebView not accepting some cookies

If you want to pass your login users data to a cookie in Webview the do it like this
The other day I had to pass my login object to particular URL
as follow.

            WebSettings settings = webViewRoi.getSettings();
settings.setDomStorageEnabled(true);
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(false);
settings.setAppCacheEnabled(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setAllowFileAccessFromFileURLs(true);

Gson gson = new GsonBuilder().disableHtmlEscaping().create();

FormsDTO formsDTO = new FormsDTO();
FormsDTOProfile dtoProfile = new FormsDTOProfile(LoginActivity.loginInfoDTO.getProfile());
formsDTO.setProfile(dtoProfile);
formsDTO.setAuthorized(true);
formsDTO.setToken(LoginActivity.loginInfoDTO.getToken());

String out123 = gson.toJson(formsDTO);

String auth2 = URLEncoder.encode(out123, "UTF-8");

String z = "userInfo=" + auth2; // here userinfo is the key of cookie
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);

cookieManager.setCookie(url, z);
webViewRoi.setWebChromeClient(new WebChromeClient());
webViewRoi.loadUrl(url);

So if you know the key name of cookie then you can pass your login object through cookie. hope it helps you.

WebView and Cookies on Android

I figured out what's going on.

When I load a page through a server side action (a url visit), and view the html returned from that action inside a Webview, that first action/page runs inside that Webview. However, when you click on any link that are action commands in your web app, these actions start a new browser. That is why cookie info gets lost because the first cookie information you set for Webview is gone, we have a seperate program here.

You have to intercept clicks on Webview so that browsing never leaves the app, everything stays inside the same Webview.

  WebView webview = new WebView(this);      
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url); //this is controversial - see comments and other answers
return true;
}
});
setContentView(webview);
webview.loadUrl([MY URL]);

This fixes the problem.

How to save a cookie in an Android webview forever?

You have to tell the CookieSyncManager to sync after it has loaded the page in question. In your sample code, the onCreate method executes completely before the WebView tries to load the page, so the sync process (which happens asynchronously) will probably complete before the page is loaded.

Instead, tell the CookieSyncManager to sync onPageFinished in the WebViewClient. That should get you what you want.

The CookieSyncManager Documentation is a good read for how to do this properly.

Here is how you could set up your WebViewClient implementation to do this for you:

webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//Users will be notified in case there's an error (i.e. no internet connection)
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}

public void onPageFinished(WebView view, String url) {
CookieSyncManager.getInstance().sync();
}
);

You would not need to tell the CookieSyncManager to sync elsewhere with this in place. I haven't tested this, so let me know if it works.

How to disable the save password dialog on an Android WebView?

Try this:

Get webview settings with .getSettings() and set this method setSavePassword(false)

public void setSavePassword (boolean save)

http://developer.android.com/reference/android/webkit/WebSettings.html#setSaveFormData(boolean)

For those using API Level 18, please see Kirk Hammet's answer below.



Related Topics



Leave a reply



Submit