Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do UIWebView and NSURLConnection share cookie storage?

I'm building an iOS app that uses Google App Engine for the backend. Google provides an HTML login site that stores an authentication cookie. If I visit that site in a UIWebView, and the user logs in, will those cookies be in storage where they will be picked up by a NSURLConnection when making a request to the same site?

like image 980
Linuxios Avatar asked Feb 16 '13 18:02

Linuxios


1 Answers

The cookie of the UIWebView will be stored in a sandboxed cookie storage accessible through NSHTTPCookieStorage sharedHTTPCookieStorage]. You can use this cookie storage in NSURLConnection in this way:

NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"__YOUR_URL__"]];
NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
[request setAllHTTPHeaderFields:headers]; //A previously created NSMutableURLRequest

Now you can normally use the NSURLRequest in a NSURLConnection and it will send the cookies created after the login in the UIWebView

like image 181
Antonio E. Avatar answered Nov 05 '22 00:11

Antonio E.