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) ??
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 ?
-
This seems related to a fastdev problem in the Android emulator, have you tried switching it off and/or testing on a device ?
-
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
-
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.