0

I've got a login system for a website, I'm starting a PHP session server side and creating a cookie in PHP which is used to revive the PHP session when I come back later on.

I'm trying to reuse my code as the back end of an iOS application, but I'm having a hard time understanding just how it's working.

I've set up the following cookie manager, as I understand this should send cookies with my requests.

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

I don't need to set any cookies from the iPhone, I just need to make sure that my requests to my php files let those scripts access the cookies.

Am I missing anything ?

Daniel
  • 23,129
  • 12
  • 109
  • 154
  • What is the problem you are having? Depending on your use case you may also want to save your cookies somewhere and reload them when your app launches. – David Brainer Dec 05 '11 at 17:37
  • I think you are absolutely right, if I relaunch my application I'm getting a different PHPSESSID in the returned list of cookies. I just need to remember a user id and a hashed string in my cookies, and make sure they're accessible from my PHP when I make requests. When I relaunch the application I am getting a new PHPSESSID - suggesting that the relaunch did in fact not persist my cookies. Could you provide some references or a sample that I could use ? – Daniel Dec 05 '11 at 18:44

1 Answers1

2

Similar questions have been asked before: Persisting Cookies In An iOS Application?

Essentially you need to save the cookie data somewhere. It could simply be a file using something like this:

[NSKeyedArchiver archiveRootObject:cookies toFile:dataFilePath];    

In which case you would return the cookie data with something like:

NSFileManager *filemgr = [NSFileManager defaultManager];
NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithFile:dataFilePath];
for (cookie in cookies) {
   [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}

Your real-world scenario will dictate the best way to handle this, but hopefully the above gets you thinking in the right direction.

Community
  • 1
  • 1
David Brainer
  • 6,223
  • 3
  • 18
  • 16
  • Hello, and thank you for you reply David, I did see that other post, and actually it isn't what I'm trying to achieve. I don't need to know anything about the cookies, all I need to do is make sure that my PHP back end web service can stick some cookies onto me and for these cookies to persist to the next time I request something from the web service. This is to make sure that I can login with my custom registration/login scripts on the server and persist the login with the help of cookies. So saving the cookie has no value. I actually needed to make sure the cookies expiry date was set – Daniel Dec 06 '11 at 10:16
  • @Daniel So, if I am understanding this right you are not worried about persisting the cookies when closing and relaunching the app. What you are dealing with is getting cookies to work at all. Correct? – David Brainer Dec 06 '11 at 14:32
  • Almost, I still need the app to maintain the fact the web service saved a cookie on me, for this I needed to make sure the cookie had a correct expiry date in the future, otherwise is was a session only cookie and the application going to the background ended the session. – Daniel Dec 06 '11 at 14:41
  • Okay, for some reason I thought that you had tried `NSHTTPCookieStorage` and needed an alternative. It sounds like your real concern was the use of session cookies rather than persistent cookies and that you have solved the issue. – David Brainer Dec 06 '11 at 15:05
  • That's the way the cookie crumbled... thanks for your involvement. Just to clear up any loose ends, I started by just adding the accept policy and I wanted to know if that was all I needed, it was, from the apps point of view, yet what I learnt here was the application going into the background ended the session, and with it all the session-only cookies. That's worthy of a post-it note – Daniel Dec 06 '11 at 15:08
  • Though David's answer was about 2 years back i wanted to know the behavior on iOS7. My app supports iOS6 and 7 both. Do i still have to store cookies manually in userdefaults which app closed and load back upon re-launch. – harshit2811 Jan 27 '14 at 10:55