Cookies setting enabled in Android WebView

I am trying to set a cookie for android browser but it doesn't work for me Here is what I tried

CookieManager cookieManager = CookieManager.getInstance(); 
CookieSyncManager.createInstance(this);

webView = (WebView) findViewById(R.id.webView);
cookieManager.setAcceptCookie(true); 
cookieManager.acceptCookie();
CookieSyncManager.getInstance().startSync();
WebSettings settings = webView.getSettings();  
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);  
settings.setDefaultZoom(ZoomDensity.FAR);
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true); 
settings.setRenderPriority(RenderPriority.HIGH);
settings.setAppCacheEnabled(true);
settings.setAllowFileAccess(true);
settings.setAllowContentAccess(true);
settings.setDomStorageEnabled(true);

      

And here is the JS

function getCookie(cname) {

    var name = cname + "=";
    var ca = document.cookie.split(';');
    alert(ca);
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
}

      

Update

This is how I set up cookies

function setCookie(cname, cvalue) {

    var domain = document.domain;
    var currentURL = document.URL;
        if (currentURL.substr(0, 4) == 'file') {
    alert("in offline cookie set");
     document.cookie = cname + "=" + cvalue + "; path=/;" + " domain=file://";
      }
      else{
     document.cookie = cname + "=" + cvalue + "; path=/;" + " domain=" + domain;
      }
}

      

+3


source to share


4 answers


I see you are trying to set file: // cookie. Try this method before creating the WebView or accessing the CookieManager: http://developer.android.com/reference/android/webkit/CookieManager.html#setAcceptFileSchemeCookies(boolean)



Please read the documentation for this method!

0


source


Try to install:



CookieManager.getInstance().setAcceptCookie(true);

      

0


source


You have to call CookieManager.getInstance().setAcceptCookie(true);

after for you to initialize your webview. I think this is your problem!

0


source


I don't have the documentation for the link, but for some (maybe security reasons) there is no way to get Cookies from the local file url scheme (ex: file: // ...) via the webView.

So I think the only solution is to create a local HttpServer (ex: NanoHttpd ), add vhost for local files.

0


source







All Articles