Where cookie is managed in phonegap app with jquery?

12,478

If you want your app to automatically manage cookies for you add this to your appDelegate.m file:

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage 
                                      sharedHTTPCookieStorage]; 
[cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; 

If you want to manage the session information by yourself, you can do all ajax requests like this:

var request =   {
                url: my_server_url,
                success: function(response, status, request) {
                    var header = request.getAllResponseHeaders();
                    var match = header.match(/(Set-Cookie|set-cookie): (.+?);/);
                    if(match)
                        my_saved_cookie = match[2];
                },
                }

if(my_saved_cookie)
    request.headers = { Cookie: my_saved_cookie };

$.ajax(request);

In my app I was managing the session cookies by myself using the second method until I discovered the first method.

Share:
12,478

Related videos on Youtube

katsuya
Author by

katsuya

Updated on May 22, 2022

Comments

  • katsuya
    katsuya almost 2 years

    My native iphone app, developed with phonegap with jquery (so its browser based), can log in to web server and once logged in users can access their resources. The server sets session id in cookie once user is authenticated.

    I do not have any trouble with this scheme but I am wondering where the cookie is stored because when I do alert(document.cookie), it returns empty string.

    Is it possible that ajax function in jquery manages the cookie internally and send it along for every request to the same domain?

  • Chris
    Chris almost 12 years
    The first method doesn't work on my iOS5 simulator, looks like Apple changed the behavior. The second method works fine.
  • iamjustcoder
    iamjustcoder over 11 years
    This works fine for ajax request. But, how should handle for image & other resources.
  • Felipe Brahm
    Felipe Brahm over 11 years
    @Chris: this method should work fine for iOS5, make sure you're adding the code on the right file.
  • Felipe Brahm
    Felipe Brahm over 11 years
    @gviswanathan: the first method should work fine for any request AFAIK
  • Ferex
    Ferex about 9 years
    But getAllResponseHeaders() method doesn't retrieve Set-Cookie, at least in Chrome