19
votes

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.

1
I use exactly that in my apps and it works fine across iPhone/iPad...Leon
Have you looked through this topic? stackoverflow.com/questions/4262636/…sleepwalker
I have seen similar topics, however, as I understand it PhoneGap can do these sorts of things ... "What this means is that you can make requests to api end points that you normally couldn't access if this were meant to be deployed for a desktop setting." wiki.phonegap.com/w/page/42450600/PhoneGap%20Ajax%20SampleRyan Lanciaux
I would recommend using LocalStorage as a viable alternative to cookies.Brian Seim at EvoDynamic Inc

1 Answers

2
votes

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.