iPhone: NSHTTPCookie is not saved across app restarts
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 request. When I restart the app, that cookie is gone and I can't use the same session anymore.
What I noticed when I used the NSHTTPCookieStorage
to look up the cookie I got from the server, is that [cookie isSessionOnly]
returns YES
. I get the impression that this is why cookies are not saved across restarts of my app. What would I have to do to make my cookie NOT session only? What HTTP headers do I have to send from the server?
You can save the cookie by saving its properties dictionary and then restoring as a new cookiebefore you go to re-connect.
Save:
NSArray* allCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:URL]];
for (NSHTTPCookie *cookie in allCookies) {
if ([cookie.name isEqualToString:MY_COOKIE]) {
NSMutableDictionary* cookieDictionary = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] dictionaryForKey:PREF_KEY]];
[cookieDictionary setValue:cookie.properties forKey:URL];
[[NSUserDefaults standardUserDefaults] setObject:cookieDictionary forKey:PREF_KEY];
}
}
Load:
NSDictionary* cookieDictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:PREF_KEY];
NSDictionary* cookieProperties = [cookieDictionary valueForKey:URL];
if (cookieProperties != nil) {
NSHTTPCookie* cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
NSArray* cookieArray = [NSArray arrayWithObject:cookie];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookieArray forURL:[NSURL URLWithString:URL] mainDocumentURL:nil];
}
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:
- Get the cookie you want to modify from
[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]
- Copy its properties to a new
NSMutableDictionary
, changing the"Expires"
value to a date in the future. - Create a new cookie from the new
NSMutableDictionary
using:[NSHTTPCookie.cookieWithProperties:]
- Save the newly created cookie using
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie newCookie]
When you reopen your app, you'll notice that the cookie has not been deleted.
Session-only cookies will expire by their nature. You can store them manually in Keychain if you really want it. I prefer Keychain to saving in UserDefaults or archiving because cookies are better be secured, just like user's password.
Unfortunately saving session-only cookies is not very helpful, the code below is just an illustration how to store cookies, but can't force the server to accept such cookies in any way (unless you can control the server).
Swift 2.2
// Saving into Keychain
if let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies {
let cookiesData: NSData = NSKeyedArchiver.archivedDataWithRootObject(cookies)
let userAccount = "some unique string to identify the item in Keychain, in my case I use username"
let domain = "some other string you can use in combination with userAccount to identify the item"
let keychainQuery: [NSString: NSObject] = [
kSecClass: kSecClassGenericPassword,
kSecAttrAccount: userAccount + "cookies",
kSecAttrService: domain,
kSecValueData: cookiesData]
SecItemDelete(keychainQuery as CFDictionaryRef) //Trying to delete the item from Keychaing just in case it already exists there
let status: OSStatus = SecItemAdd(keychainQuery as CFDictionaryRef, nil)
if (status == errSecSuccess) {
print("Cookies succesfully saved into Keychain")
}
}
// Getting from Keychain
let userAccount = "some unique string to identify the item in Keychain, in my case I use username"
let domain = "some other string you can use in combination with userAccount to identify the item"
let keychainQueryForCookies: [NSString: NSObject] = [
kSecClass: kSecClassGenericPassword,
kSecAttrService: domain, // we use JIRA URL as service string for Keychain
kSecAttrAccount: userAccount + "cookies",
kSecReturnData: kCFBooleanTrue,
kSecMatchLimit: kSecMatchLimitOne]
var rawResultForCookies: AnyObject?
let status: OSStatus = SecItemCopyMatching(keychainQueryForCookies, &rawResultForCookies)
if (status == errSecSuccess) {
let retrievedData = rawResultForCookies as? NSData
if let unwrappedData = retrievedData {
if let cookies = NSKeyedUnarchiver.unarchiveObjectWithData(unwrappedData) as? [NSHTTPCookie] {
for aCookie in cookies {
NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(aCookie)
}
}
}
}
I believe it's up to the server to decide whether or not the cookie is session-only, you can't do anything about it.
Swift way
Store:
static func storeCookies() {
let cookiesStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
let userDefaults = NSUserDefaults.standardUserDefaults()
let serverBaseUrl = "http://yourserverurl.com"
var cookieDict = [String : AnyObject]()
for cookie in cookiesStorage.cookiesForURL(NSURL(string: serverBaseUrl)!)! {
cookieDict[cookie.name] = cookie.properties
}
userDefaults.setObject(cookieDict, forKey: cookiesKey)
}
Restore:
static func restoreCookies() {
let cookiesStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
let userDefaults = NSUserDefaults.standardUserDefaults()
if let cookieDictionary = userDefaults.dictionaryForKey(cookiesKey) {
for (cookieName, cookieProperties) in cookieDictionary {
if let cookie = NSHTTPCookie(properties: cookieProperties as! [String : AnyObject] ) {
cookiesStorage.setCookie(cookie)
}
}
}
}