Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Passing passing cookie across with webView

I'm having trouble passing an authentication cookie across with a webview (note this problem is an Android problem only because with iOS the cookie is passed across automatically and I don't need to retrieve it at all).

I retrieve the cookie at the time I log in to my server:

Titanium.App.Properties.setString("Cookie", login.getResponseHeader("Set-Cookie"));

I have checked that the cookie is being retrieved and stored ok. It's value is:

SessionID=CZYA90NB7L; path=/

However, when I try and pass this across to the webView using the beforeLoad event I get an error. This is my code:

webView = Titanium.UI.createWebView({
    url : myURL;
});

webView.addEventListener('beforeload',function(e){
var cookie = Titanium.App.Properties.getString("Cookie");
webView.evalJS("document.cookie = " + cookie + ";");
});

The error I get when I do this is:

W/ActivityManager( 60): Launch timeout has expired, giving up wake lock!

Has anyone tried this (or something very similar) and got it working (for Android) ??

— asked November 1st 2011 by Richard Shergold
  • beforeload
  • cookies
  • mobile
  • webview
0 Comments

4 Answers

  • This seems related to a fastdev problem in the Android emulator, have you tried switching it off and/or testing on a device ?

    FastDev Wiki Reference

    — answered November 6th 2011 by Marco Del Tongo
    permalink
    5 Comments
    • I will try that Marco, thanks.

      — commented November 9th 2011 by Richard Shergold
    • Hi Marco. I disabled fastdev and still get the same error. I'd really just like to know if it is possible to load a webView with a cookie (in Android) so that I know if I'm wasting my time or not. Some working code would be great but as yet I've not seen any from anyone. This is a bit of a show stopper for us unfortunately as we need the app to be working on Android just as it does on iOS (and in iOS all this works fine).

      — commented November 9th 2011 by Richard Shergold
    • Richard did you find a solution?

      — commented September 18th 2012 by B R
    • Did anyone ever get this working?

      — commented February 11th 2013 by Chad Stachowicz
    • Yes I did. It wasn't easy but I ended up building a custom SDK by merging in the changes here: https://github.com/appcelerator/titanium_mobile/pull/1162

      — commented February 12th 2013 by B R
  • This seems related to a fastdev problem in the Android emulator, have you tried switching it off and/or testing on a device ?

    FastDev Wiki Reference

    — answered November 6th 2011 by Marco Del Tongo
    permalink
    0 Comments
  • Midnight VIP es una aplicación gratuita para móviles que llega para revolucionar el ocio nocturno de Madrid, presentando ofertas y descuentos de un amplio listado de locales. Midnight VIP hace posible que te puedas apuntar en las listas de las discotecas. Disponible gratuitamente en App Store y Google Play.

    http://www.midnightvip.com

    — answered June 12th 2013 by a a
    permalink
    0 Comments
  • I know this is an old post but I had a similar issue with evalJS on Android, API >=16. The solution was to fire an event to pass the data into the WebView, namely Ti.App.fireEvent, Titanium's pub / sub pattern. You can then listen for this event on the html page and set the document.cookie.

    Please note, Titanium's pub/sub pattern will only work if load your content locally.

    Great, you’ve now got your cookie set in the WebView but you’ll notice this cookie will not be included in subsequent HttpXmlRequests. The reason is the Origin header.

    If you look at the request in fiddler you’ll see your login request does not have an Origin header set while any request made from the WebView will have the Origin header set to file:// (this was true in my case). The solution is simply, when you make your login request use the setRequestHeader function to add the Origin header.

    — answered August 21st 2014 by Adrian Holmes
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.