Enable cookies on PhoneGap Android App

5,096 views
Skip to first unread message

Santiago Gonzalez Toral

unread,
May 6, 2012, 9:25:55 PM5/6/12
to phonegap
Hi,

I develop a mobile web app using jquerymobile, html 5 & css3, java
servlets on server side. and i build the app using phonegap build. but
i have a problem when running my app on android devices. The cookies
are not enabled on the webview (running it on the browser works well),
even if the device has enable cookies. I read some posts on internet
and may be exists a solution, but i found just for iOS devices.. Can
anyone help me ?

Thanks

hakim...@gmail.com

unread,
May 6, 2012, 11:47:59 PM5/6/12
to phonegap
In Android, the cookies are not carried over from HttpClient to
WebView, therefore, you have to manually read the cookie from
HttpClient, and set it to WebView.

Hope it helps.

Reference:
http://stackoverflow.com/questions/1652850/android-webview-cookie-problem

On May 7, 9:25 am, Santiago Gonzalez Toral <hernangt12...@gmail.com>
wrote:

Santiago Gonzalez Toral

unread,
May 8, 2012, 11:31:52 AM5/8/12
to phonegap
Thanks for the reply

I'm not using native code for developing my app on android devices, I
just use html5 and build the app using PhoneGap Build to get .apk ..
May i have to use another method for session tracking, or there is
some method to make the webView accept cookies without coding java? ..
i Have no problems on Blackberry devices

Thomas Dall'Agnese

unread,
May 3, 2015, 10:11:56 PM5/3/15
to phon...@googlegroups.com, hernan...@gmail.com
Still same issue as of today.
Cookies are not accessible from webview's XHR requests, which is a pain to communicate with old websites.

Jesse Monroy

unread,
May 5, 2015, 4:10:44 PM5/5/15
to phon...@googlegroups.com, hernan...@gmail.com
Phonegap apps are NOT webbrowsers. In addition, Google and Apple both discourage the use of Apps and wrappers to websites. So what are you trying to build?

FWIW, there are plenty of third-party tracking packages that do NOT use cookies.

Jesse

Thomas Dall'Agnese

unread,
May 5, 2015, 7:23:29 PM5/5/15
to phon...@googlegroups.com, hernan...@gmail.com
There ARE valid use cases where we need to read/write cookies.

In my case, I am building a web interface for a heavy old-fashioned desktop website.
Considering transmitting these big HTML pages and parsing them is terrible performance-wise, I built a "mobilizer proxy" service that translates heavy HTML into light JSON with only the required data.
However, I do not want the users to login via the proxy, but I prefer them to send the username/password to the original website directly and use the SESSIONID cookie set by the website when calling the mobilizer proxy for subsequent calls (for security reasons).
So I need to login from the cordova app to the old-fashioned website and retrieve the SESSIONID cookie that I can use in the mobilizer proxy.

Unfortunately, it is not possible in Cordova apps to manage the cookies directly from the JavaScript side (although it seems to be managed in the background, just not accessible to the JavaScript side).
So I found a workaround to that: I created a Cordova plugin that logins natively (using java.net.HttpClient for example in Android) and returns the SESSIONID to the JavaScript side (as it's possible to read/write the cookies from the native side).
That's far from ideal, but the best workaround I found so far.

Thanks,
Thomas

jcesarmobile

unread,
May 6, 2015, 2:27:08 AM5/6/15
to phon...@googlegroups.com, hernan...@gmail.com
Phonegap is supposed to handle the cookies, if it isn't working properly you can file an issue on the cordova JIRA page giving all the possible details (device, android version, cordova version, etc)

Jesse Monroy

unread,
May 6, 2015, 7:46:06 PM5/6/15
to phon...@googlegroups.com, hernan...@gmail.com
Given this thread was over two years old, and the website in question will change or die.
The word you are looking for is REFACTOR.

Best of Luck
Jesse



On Tuesday, May 5, 2015 at 4:23:29 PM UTC-7, Thomas Dall'Agnese wrote:
There ARE valid use cases where we need to read/write cookies.

In my case, I am building a web interface for a heavy old-fashioned desktop website.
::::SNIP::::

Kerri Shotts

unread,
May 7, 2015, 2:47:52 PM5/7/15
to phon...@googlegroups.com, jess...@gmail.com, hernan...@gmail.com
In the enterprise, websites tend to stick around until someone screams "Security risk". That's the only fast and effective way to kill them I've found. :-)

Kerri Shotts

unread,
May 7, 2015, 2:53:33 PM5/7/15
to phon...@googlegroups.com, thomas.d...@gmail.com, hernan...@gmail.com
Out of curiosity, how is the cookie being set from the old website? That is, what's the domain, path, security, etc. of the cookie? If it's being passed as HttpOnly or the domains are different, that'd be a good reason as to why you wouldn't be able to access the cookie. 

It might be useful to see some code of what you expect should work (and what you're doing to work around it)... that might make it easier for us to wrap our heads around it. I've yet to have any problems working with cookies in PhoneGap, so I'm very curious as to why you're having issues.

Thomas Dall'Agnese

unread,
May 7, 2015, 8:02:16 PM5/7/15
to phon...@googlegroups.com
That's a pretty common issue with phonegap apps.
The workaround on iOS (hacking Cordova): http://stackoverflow.com/questions/3709315/phonegap-cookie-based-authentication-php-not-working-webview

Yes we're talking about remote sites on different domains, and pretty common authentication back in the 2000's : POST username/password, response header contains "Set-Cookie" that we can use for being authenticated on subsequent requests.

Same question on Stack with code sample : http://stackoverflow.com/questions/30011315/read-write-the-cookies-from-an-xhr-request-response-phonegap-cordova

Jesse Monroy

unread,
May 8, 2015, 5:26:12 PM5/8/15
to phon...@googlegroups.com, hernan...@gmail.com
@jcesarmobile,
Phonegap may be able to handle cookies, but where are they going to be tested. If Google Play and Apple iTunes will not accept your App, how can you test it? who is going to test it? What is this App for?

Jesse

jcesarmobile

unread,
May 9, 2015, 10:02:51 AM5/9/15
to phon...@googlegroups.com
Why wouldn't they approve your apps? For using cookies?

Jesse Monroy

unread,
May 9, 2015, 5:36:27 PM5/9/15
to phon...@googlegroups.com
@jcesarmobile,
I didn't make the rules, Google and Apple did. They don't want wrappers on websites.
If your server, which might happen, to provide webservice also provides cookies - that's another issue.
However cookies, as we know them intended for webservice, not Apps.

If however, you are looking at tracking user behaviour and user use, then there are plenty of alternative services, perhaps you should look into them. The term you are looking for is mobile analytics.

Google: mobile analytics
Bing: mobile analytics

Enjoy your day
Jesse

Jesse Monroy

unread,
May 9, 2015, 6:06:04 PM5/9/15
to phon...@googlegroups.com
@jcesarmobile,
I just realized you may be more  successful if you use the plugin, InAppBrowser (this link is 3.3). I have no experience with this, perhaps someone else does - that is not a vocal as I ;-)


Jesse

On Saturday, May 9, 2015 at 7:02:51 AM UTC-7, jcesarmobile wrote:

jcesarmobile

unread,
May 10, 2015, 5:13:08 AM5/10/15
to phon...@googlegroups.com
First of all, I'm not the one having problems with the cookies
Second, who talked about wrapped websites? My answer was to the user having problems with the cookies when using XHR calls, they are supposed to work with phonegap
Reply all
Reply to author
Forward
0 new messages