Persisting Cookies In An iOS Application?
Asked Answered
W

2

55

I am going to use NSHTTPCookieStorage in an iOS App to manage cookies that are retrieved from a url, and I understand that it will manage cookies during your application's runtime. However, I was wondering if it's possible to persist cookies after the application has closed. And then read those cookies again when the app is opened again. Does NSHTTPCookieStorage persist cookies between app uses? Or just during the applications runtime? Do I need to use CoreData to persist these cookies?`

Whereabouts answered 4/1, 2011 at 19:54 Comment(0)
M
46

You need to re-set the cookies when your app is loaded. I use code like this:

NSData *cookiesdata = [[NSUserDefaults standardUserDefaults] objectForKey:@"MySavedCookies"];
if([cookiesdata length]) {
    NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesdata];
    NSHTTPCookie *cookie;

    for (cookie in cookies) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }
}

and it works just fine.

Membranophone answered 4/1, 2011 at 20:1 Comment(3)
Is MySavedCookies already there? Or do I have to store that when my app is about to go inactive?Whereabouts
You have to save it yourself - It's just where I save mine.Membranophone
Storing cookies in user defaults may be a security flaw as all plist can be accessed via tools like iFunBox etc. They should be encrypted but the best behavior is to follow @Winnow answer.Quadragesima
W
117

You shouldn't need to persist the cookies yourself as suggested in the other answer. NSHTTPCookieStorage will persist the cookies for you but you need to ensure that the cookies have an expiry date set on the server-side.

Cookies without an expiry date are considered 'session only' and will get cleared when you restart the app. You can check the 'session only' situation via a BOOL property in NSHTTPCookie. This is standard cookie stuff and not something specific to iOS.

Winnow answered 3/1, 2012 at 13:54 Comment(4)
Came here looking for this clarity, thanks. Interesting that it's cleared when the app is restarted. You can restart Chrome on desktop and still have a session open.Shannonshanny
+1 I understand now why my cookies are no more there :). Thanks !!Plucky
@Winnow I am trying to encrypt the cookie files in iOS cordova application. Its working but only when app is in foreground. When I switch the app or press home button, new cookie file is created with same data. Can anybody guide me how I can encrypt the cookie files?Waxbill
You made my day, thankyou!Episternum
M
46

You need to re-set the cookies when your app is loaded. I use code like this:

NSData *cookiesdata = [[NSUserDefaults standardUserDefaults] objectForKey:@"MySavedCookies"];
if([cookiesdata length]) {
    NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesdata];
    NSHTTPCookie *cookie;

    for (cookie in cookies) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }
}

and it works just fine.

Membranophone answered 4/1, 2011 at 20:1 Comment(3)
Is MySavedCookies already there? Or do I have to store that when my app is about to go inactive?Whereabouts
You have to save it yourself - It's just where I save mine.Membranophone
Storing cookies in user defaults may be a security flaw as all plist can be accessed via tools like iFunBox etc. They should be encrypted but the best behavior is to follow @Winnow answer.Quadragesima

© 2022 - 2024 — McMap. All rights reserved.