In my iPhone app, I want to be able to reuse the same server-side session when my app restarts. A session on the server is identified by a cookie, which is sent on each requ
I have upvoted @TomIrving's answer and am elaborating here because many users will not see the very important comment in which he says:
"You need to set an expiration date, otherwise the cookie is assumed to be session only."
Basically, the cookie will be deleted when you close your app UNLESS the cookie has an expiration date in the future.
You don't need to store and restore the cookies in and from NSUserDefaults
if you have control over the server and can ask it to set the "Expires" header to something in the future. If you don't have control over the server or do not wish to override your server's behavior, you can 'trick' your app by changing the expiresDate
from within it:
[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]
NSMutableDictionary
, changing the "Expires"
value to a date in the future.NSMutableDictionary
using: [NSHTTPCookie.cookieWithProperties:]When you reopen your app, you'll notice that the cookie has not been deleted.