iOS store cookies in HTTPClient, Android stores in Webview

You must Login before you can answer or comment on any questions.

iOS 5.0, Android 2.2,2.3 and Titanium SDK 1.8.2

When log into site by webview, on iOS webview doesn't store cookies, but HTTPClient store. On Android, webview stores cookies, but HTTPClient doesn't sore.

// Webview
var webView = Ti.UI.createWebView();
win.add(webView);
webView.url = 'http://myweb.com/log_in';
webView.addEventListener('load',function(e){
    var cookies = webView.evalJS("document.cookie").split(";");
    for (i = 0; i <= cookies.length - 1; i++) {
        Ti.API.info( "cookie => " + cookies[i] );
        // iOS
        // cookie => 
        // cookie is blank even though logged in.
        // On Android
        // cookie => _session=fa12eaoiiudoiuao....
        // return cookies
    };
});
 
// HTTPClient
var xhr = Ti.Network.createHTTPClient();
xhr.onload = function(e) {
     setCookie = xhr.getResponseHeader('Set-Cookie');
     Ti.API.info('setCookie => ' + setCookie);
        // iOS
        // setCookie => _login_session=adabeoiu....
        // return cookies
        // Android
        // setCookie => undefined
};
xhr.open('GET', 'http://myweb.com/log_in');
// xhr.setRequestHeader('User-Agent', httpClientUserAgent);
xhr.send();
I manage to get cookies, on iOS get cookies from HTTPClient and on Android get cookies from webview.

Is there a good way to get cookies both iOS and Android?

1 Answer

It works for me. Let make a sample code with a webview to load http://bbc.com:

var win = Ti.UI.createWindow({
    backgroundColor : 'white'
});
 
var webView = Ti.UI.createWebView();
win.add(webView);
webView.url = 'http://bbc.com';
 
win.open();
 
webView.addEventListener('load', function(e) {
    var cookies = webView.evalJS("document.cookie");//.split(";");
    Ti.API.info(cookies);
 
    cookies = cookies.split(";");
 
    for( i = 0; i <= cookies.length - 1; i++) {
        Ti.API.info("cookie => " + cookies[i]);
    };
});
Run it you would see the cookies like below:
[INFO] cookie => _qubitTracker=1332329477961.96906
[INFO] cookie =>  _qPageNum_bbccom=0
[INFO] cookie =>  _qsst=1332329571227
[INFO] cookie =>  _qst=%5B3%2C0%5D
...

— answered 1 year ago by Minh Nguyen
answer permalink
1 Comment
  • Thanks for your reply. I tested your code and confirmed that you are right.

    Actually, I've tested my code localhost Rails application.

    It seems that webview or HTTPClient on simulator can't share or store cookies correctly on local site.

    Does anyone know about this?

    — commented 1 year ago by DIGITAL SQUAD

Your Answer

Think you can help? Login to answer this question!