简体   繁体   中英

Cookies are not being stored in Android Webview

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;
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM