Friday, November 26, 2010

CookieManager and removeAllCookie problem

There was a requirement to send a special cookie to a web app through a WebView component in one of the applications I'm building recently.

I could set a cookie by using Android's CookieManager with the code below. This code worked well on the emulator (Donut, Eclair and Froyo) and on my devices HTC Magic with original Donut ROM and custom CyanogenMOD 6.0.0 ROM.

CookieSyncManager.createInstance(this);
CookieManager cm = CookieManager.getInstance();
cm.removeAllCookie();
cm.setAcceptCookie(true);
cm.setCookie(myCookieURI, "cookie_name=value");
CookieSyncManager.getInstance().sync();

The problem was when I tested this on some devices: Galaxy S and HTC Desire both running Froyo. The cookie was not being sent on those devices. After testing some modifications on the code I've found removeAllCookie() was removing the newly set cookie too. (Looks like there is a synchronization problem with CookieManager here?)

Removing removeAllCookie() from the code made everything work okay again. Hopefully, removing all cookies was not a requirement in this application. ;)

CookieSyncManager.createInstance(this);
CookieManager cm = CookieManager.getInstance();
cm.setAcceptCookie(true);
cm.setCookie(myCookieURI, "cookie_name=value");
CookieSyncManager.getInstance().sync();

7 comments:

chansara said...

I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information. UC News Download

Uzair Rajput said...

Hi, I find reading this article a joy. It is extremely helpful and interesting and very much looking forward to reading more of your work.. ACMarket

francis beth said...

I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day! Action Link Wireless

BorkenSKY said...

Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article. Emus4u

DARYA KHAN said...

Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. Cydia Impactor

Core vpro said...

Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information.. iFile IPA

2009 DREAM said...

Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!! Download iOS 11.3