Where cookie is managed in phonegap app with jquery?
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.
Related videos on Youtube
katsuya
Updated on May 22, 2022Comments
-
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 almost 12 yearsThe first method doesn't work on my iOS5 simulator, looks like Apple changed the behavior. The second method works fine.
-
iamjustcoder over 11 yearsThis works fine for ajax request. But, how should handle for image & other resources.
-
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 over 11 years@gviswanathan: the first method should work fine for any request AFAIK
-
Ferex about 9 yearsBut getAllResponseHeaders() method doesn't retrieve Set-Cookie, at least in Chrome