Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing and sending of http cookies not working in PhoneGap app

I'm developing a PhoneGap app in Xcode that communicates with a server using a web api. The api accepts standard http requests. As part of the user authentication process a cookie is set that is supposed to be transmitted in the http header with each subsequent request to the api.

During the login call to the api, I can clearly see that a cookie is being sent back as part of the response header. However, it doesn't seem to get stored, nor does it get sent in the http request header with each new api call.

To make calls to the api, I'm using jQuery's $.getJSON call.

I found the following question and answer: phonegap: cookie based authentication (PHP) not working [webview]

The above answer suggests setting the NSHTTPCookieStorage cookieAcceptPolicy to NSHTTPCookieAcceptPolicyAlways in the app delegate's init. I have tried that and it doesn't seem to solve the issue. Also, according to Apple's documentation, the default cookieAcceptPolicy is NSHTTPCookieAcceptPolicyAlways.

Suggestions on how to get cookie storage and sending working are highly appreciated.

like image 888
Bringo Avatar asked Dec 09 '11 17:12

Bringo


1 Answers

the CookieStorage should and does save the cookie, to be used on subsequent requests. it even adds them automatically if you use, for example, NSURLRequest... but only if it's for the same domain that is set on the cookie.

I've ran into similar problems many times before,

possible causes can be:

1) the cookie has a different domain that what you are sending on the request, possibly due to a redirect... for example if requests is sent to http://www.somesite.com, which is actually redirecting to http://somesite.com (and that is the real domain name, cookie will be saved for the sometime.com url/domain and now www.somesite.com

2) possibly the "session only" and "expiration date" headers are set incorrectly on the cookie, which causes the cookie storage to discard them

hope this helps, know from experience cookie storage can be frustrating at times.

like image 52
Miki Bergin Avatar answered Sep 21 '22 23:09

Miki Bergin