CookieManager and CookieSyncManager not syncing cookies to webview in ICS (works on all previous API levels)
Asked Answered
H

4

17

Background:

I'm acquireing cookies through my app and use them internally by making requests to http. Users can switch to webview any given time, in which case I sync/inject cookies to webView.

Code for synchronizing cookies to webview using CookieSyncManager:

CookieSyncManager.createInstance(a);
CookieManager cm = CookieManager.getInstance();
cm.setCookie(domain, cookieName + "=" + cookieValue);
CookieSyncManager.getInstance().sync();

This works on all API levels except API 15+ (Android 4.0, Ice Cream Sandwich). The CookieManager API is not deprecated.

What is causing this problem and are there any workarounds?

Halette answered 14/3, 2012 at 9:12 Comment(0)
T
3

You can try the Apache DefaultHttpClient to do this work for you, I don't think it been changed in Ice Cream Sandwich.

I found this sample, but there is lots more in here

Tic answered 22/3, 2012 at 8:44 Comment(2)
This could actually work... Overriding the WebViewClient.shouldOverrideUrlLoading to use the Apache DedfaultHttpClient to acquire webpage using cookies and inject the HTML into webview with webView.loadData (meaning, you handle all the logic and webview is used only for displaying/rendering HTML)Embassy
@Beuj found another oneTic
M
4

I had the same problem recently which I found was my mistake. The problem was the way I set the domain (but it worked till API 15). Try to prefix the domain with the dot: ".company.com" instead of "company.com".

Morion answered 3/4, 2012 at 10:52 Comment(3)
This worked, but I found that it caused the same problem in reverse - it'd work in ICS, and not pre-ICS. I had to do a quick if-else to use ".domain.com" if the app was running on Ice Cream Sandwich, and "domain.com" if not. Strange.Tangelatangelo
After tried lot of various ways, Finally Worked for me by just adding a dot before domain cookieManager.setCookie("."+domain, cookie);Lindquist
What does the domain variable contain?Worker
T
3

You can try the Apache DefaultHttpClient to do this work for you, I don't think it been changed in Ice Cream Sandwich.

I found this sample, but there is lots more in here

Tic answered 22/3, 2012 at 8:44 Comment(2)
This could actually work... Overriding the WebViewClient.shouldOverrideUrlLoading to use the Apache DedfaultHttpClient to acquire webpage using cookies and inject the HTML into webview with webView.loadData (meaning, you handle all the logic and webview is used only for displaying/rendering HTML)Embassy
@Beuj found another oneTic
P
0

I wasn't injecting cookie from the client, but i found ICS cookie weren't saving.

One work around is to use the local storage instead of cookies. You don't need a cookie sync manager. this works on ics and v2+ android.

In my case I didn't have a domain , so the above didn't seem relevant - I was using a local html file in the asset folder of the app.

this works on ics and v2 android

enjoy

java

// java
WebSettings webSettings = myWebView.getSettings();
webSettings.setDomStorageEnabled(true);   // localStorage


// e.g., if your package is www.myapp.whatever;
webSettings.setDatabasePath("/data/data/www.myapp.whatever/databases/");

html

// javascript

function createCookie(name,value,days,path) {
    localStorage.setItem(name,value);
}

function readCookie(name) {
    return localStorage.getItem(name);
}
Pemba answered 2/8, 2013 at 1:41 Comment(0)
A
0

Here is your answer : In ICs, can`t get cookie

Its the domain which is causing the issue.

for all version after 15+ you have to you have to use **.**domain.com , instead of domain.com.

Aprilette answered 13/3, 2014 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.