user2095039
user2095039

Reputation: 1

Cookie on Android 2.2 is not available in WebView

I made a Android App with minimum required API 8. The users are authenticated on my backend with a cookie. This works on every device that has an api level 9 or higher. The Cookie is saved with CookieManager:

CookieManager cManager = CookieManager.getInstance();
CookieSyncManager.createInstance(LauncherApplication.getAppContext());
cManager.setAcceptCookie(true);
cManager.setCookie(".xxxxxxxx.xx", "MobileGuid=" + guid);
CookieSyncManager.getInstance().sync();

I check if this Cookie is available also with CookieManager:

public static boolean hasCookie(){
    CookieManager cManager = CookieManager.getInstance();
    String cookieString = cManager.getCookie(".xxxxxxx.xx");
    if(cookieString != null && cookieString.contains("MobileGuid")){
        return true;
    }
    return false;
}

This returns always true, but the Cookie is just available in the WebView if the Android Version is higher then 2.2. (I checked this with phpinfo)

My WebView configuration looks like this:

String databasePath = LauncherApplication.getAppContext().getApplicationContext().getDir("database",
            Context.MODE_PRIVATE).getPath();

    WebSettings mainWebSettings = mainWebView.getSettings();
    mainWebSettings.setJavaScriptEnabled(true);
    mainWebSettings.setAppCacheEnabled(true);
    mainWebSettings.setDatabaseEnabled(true);
    mainWebSettings.setDomStorageEnabled(true);
    mainWebSettings.setDatabasePath(databasePath);

Upvotes: 0

Views: 305

Answers (2)

adeous
adeous

Reputation: 143

Like the answer of Hylianpuffball the idea is to add a character below your domain name. I think that the CookieManager is looking for something like .domain.com and kind of substring is done to get the domain name.

Just add a dot or any character and it will be working

Upvotes: 0

Hylianpuffball
Hylianpuffball

Reputation: 1561

When you call cManager.setCookie(), try passing

cManager.setCookie("a.xxxxxxxx.xx", "MobileGuid=" + guid + "; domain=xxxxxxx.xx");

So I've added an "a" to the first parameter (literally the character 'a'), and added "domain=xxxxxxx.xx" to the cookie itself.

Your code as written seems to be all correct, and I had a similar struggle with API 9 devices not sending cookies while API 14 devices did. I'm afraid I don't know exactly why it works, but it was the only thing that helped me.

Upvotes: 1

Related Questions