0

I have a pure HTML+CSS+JS app that I'm trying to embed into an Android app using the Webview. The app all works fine except, the cookies aren't stored. I've already tried the suggestions in these QA, but none of them seem to work:

Make Android WebView not store cookies or passwords

Cookie doesn't work properly in webview in android

Webview cannot accept cookies

Android WebView HTTP Cookies not working in API 21

This is how I initiate the WebView in my activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.acceptCookie();
    cookieManager.setAcceptFileSchemeCookies(true);

    WebView webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);

    //webView.getSettings().setPluginState(PluginState.ON);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setAllowContentAccess(true);
    if (Build.VERSION.SDK_INT >= 16) {
        webView.getSettings().setAllowFileAccessFromFileURLs(true);
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    }

    webView.loadUrl("file:///android_asset/index.html");

}

And here is the JavaScript createCookie() method that flawlessly works in a normal browser:

function createCookie(name, value, days) 
{
    value = value.replace(';', COOKIE_ENCODER);

    if (days>=0) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    //else var expires = "";


    document.cookie = name + "=" + value + expires; // + "; path=/";
}

I also tried using CookieSyncManager though Android Studio suggested that its depreciated. The console doesn't show any errors when the createCookie() method is called, it just doesn't store the cookie.

Edit

Below is the code for readCookie() function which is used to read the cookie in JavaScript. I think its equally possible that the cookie is being stored, but the browser is having issues in reading it back:

function readCookie(name) 
{
    //name = name.replace(';',COOKIE_ENCODER);
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0)
        {
            s = c.substring(nameEQ.length, c.length);
            s = s.replace(COOKIE_ENCODER,';');
            return s;
        }
    }
    return null;
}
Prahlad Yeri
  • 3,567
  • 4
  • 25
  • 55

1 Answers1

2

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

  • Thanks, Greg. Yes, that's what I actually ended up doing in my app, but there has to be a way to save cookies too, since we want to emulate a web browser with the Webview. – Prahlad Yeri Jun 03 '17 at 05:24
  • Did you try initializing the CookieManager onPageFinished, like what was recommended for the deprecated CookieSyncManager? https://stackoverflow.com/questions/8390156/how-to-save-a-cookie-in-an-android-webview-forever – Greg Hardin Jun 03 '17 at 07:29
  • I did, but Android Studio says that `CookieSyncManager` is depreciated. But I did it anyway and even that failed to work (still no cookie saved in the browser). Also, I've tested the app on both my device and emulator. – Prahlad Yeri Jun 03 '17 at 08:30