3

I used AFHTTPClient and seems it stored cookies in the box. So when I was logged in. and then reload my app, the cookies were in application automatically. Maybe I confused about this. But seems it works in the way I described.

Now I use this code:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager POST:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {  
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];

But when I restart my application I get message with wrong token when I try request some of my api method.

So how can I store my cookies using the code above. I use this code for all my request for login, logout and other api services. If I don't restart it works good, but i rerun it seems cookies are went from me.

How can I solve this?

I found this answer link but I am not sure how to use it.

I have checked cookies.

If I don't rerun application the cookies are the same:

<NSHTTPCookie version:0 name:"...." value:"....." expiresDate:(null) created:2014-02-27 19:24:29 +0000 (4.15222e+08) sessionOnly:TRUE domain:"...." path:"/" isSecure:FALSE>

But if I rerun app the cookie are changed.

Oh I understood, the app does not save cookies, seems I need to write this functional. Do you have good suggestion how to do it?

Community
  • 1
  • 1
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • I think this is only an issue while debugging in Xcode - see http://stackoverflow.com/a/15465853/1445366 - have you had any issues on an actual device while it's not hooked up to Xcode? – Aaron Brager Feb 27 '14 at 19:56
  • @AaronBrager you think? no I have not tested yet. So you guess the cookies have to be stored on the devices. I found this url how to save cookies and then recreate it, but I suppose that AFNetworking 2 has this feature in the box. http://stackoverflow.com/questions/14387662/afnetworking-persisting-cookies-automatically – Matrosov Oleksandr Feb 27 '14 at 20:00
  • You should not store cookies in NSUserDefaults as shown in that answer. You should use NSHTTPCookieStorage (which is what is happening now). – Aaron Brager Feb 27 '14 at 20:02
  • One other possibility - your cookies don't have an expiration date, so NSHTTPCookieStorage may be discarding them when the app closes (I'm not sure about this). If this is the case, you can resolve by having your server set an expiration date on the cookie - say, one year in the future. – Aaron Brager Feb 27 '14 at 20:02
  • @AaronBrager I think the AFNetworking use this under the hood, I can check the cookies after first login request. But when I rerun it the NSHTTPCookieStorage is empty. Maybe only on the simulator, still have not check on the real device. but in any case I can't test app then on simulator because of many requests. ( – Matrosov Oleksandr Feb 27 '14 at 20:06

1 Answers1

1

From Persisting Cookies in an iOS Application:

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.

AFNetworking does not do anything with cookies under the hood. It does use NSURLConnection, and NSURLConnection uses NSHTTPCookieStorage.

According to your log:

<NSHTTPCookie version:0 name:"...." value:"....." expiresDate:(null) created:2014-02-27 19:24:29 +0000 (4.15222e+08) sessionOnly:TRUE domain:"...." path:"/" isSecure:FALSE>

you have session only cookies ("sessionOnly:TRUE").

You need to add an expiration date on the server end so that NSHTTPCookieStorage doesn't throw them away when your session ends.

This is not anything AFNetworking-specific; it's just how cookies work.

Community
  • 1
  • 1
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287