Skip to content Skip to sidebar Skip to footer

How To Save A Cookie In An Android Webview Forever?

With my code below, I have been able to save a cookie, but as soon as I close the application the cookie disappears. How is this caused and how can I solve it? package com.jkjljkj

Solution 1:

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(newWebViewClient() {
    publicvoidonReceivedError(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();
    }

    publicvoidonPageFinished(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.

Solution 2:

.sync() is to force a imediate sync ,and must be called after page load cause it sync RAM with cache , so cookie must be in ram before calling it .

System automatically sync it every 5 mintues if you use this scheme

onCreate: 
CookieSyncManager.createInstance(context)

onResume:
CookieSyncManager.getInstance().startSync()

onPause:
CookieSyncManager.getInstance().stopSync()

I think you did not waited 5 mintues so system save cookie.

Solution 3:

For those who are facing Problems with CookieManager class to make cookie persist even after closing the app, they should try the flush() function of CookieManager.

Please note that i haven't tried this, so if it works then please let me know too.

According to android documentation

void flush()

Ensures all cookies currently accessible through the getCookie API are written to persistent storage. This call will block the caller until it is done and may perform I/O.

Also in CookieSyncManager documnentation it is written that:

This class was deprecated in API level 21. The WebView now automatically syncs cookies as necessary. You no longer need to create or use the CookieSyncManager. To manually force a sync you can use the CookieManager method flush() which is a synchronous replacement for sync(). CookieSyncManger link

Solution 4:

Works perfectly for API 20+

        myWebView.setWebViewClient(newWebViewClient(){
            publicvoidonReceivedError(WebView webView, int errorCode, String description, String failingUrl){
                Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();

            }

            publicvoidonPageFinished(WebView webView, String url){
                CookieManager.getInstance().flush();
            }

        }
        
        );

Post a Comment for "How To Save A Cookie In An Android Webview Forever?"