5
votes

For these purposes, let say I have an application which contains a single UIWebview.

I'm loading a request to any of these sites: facebook, gmail, twitter, etc., and receive a login screen.

The thing is - that if I log in and relaunch the app, I will have to go through the login screen again.

In Mobile Safari/Google app for example I would retain my login cookie/session details/whatnot and wouldn't need to enter my user credentials again. That is the behavior I want to have.

Stuff I tried:

  • sending cookies from the NSHTTPCookieStorage via the NSURLRequest.
  • Persisting the cookies myself to NSUserDefaults, but no luck.

How can I achieve the behavior Mobile Safari & Google app has?

1
It's definitely possible, as I see now that Google app does it. But how?Gilbert
Hi @Gilbert I have been trying to achieve the same result as you specified. You marked the answer below as Accepted. I know how to save and/or load cookies when application closes/launches but Could you please tell me how do you get Cookies from an NSURLRequest?ShayanK
NSURLConnection automatically saves cookies that you receive view NSURLRequest, unless otherwise specified.Gilbert

1 Answers

5
votes

The cookies should be archived and persisted when the app is terminated and later reloaded when the app becomes active. You can achieve this by:

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:url mainDocumentURL:nil]; // where cookies is the unarchived array of cookies