Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing HTTP Cookies on iPhone

I want to port a python app that uses mechanize for the iPhone. This app needs to login to a webpage and using the site cookie to go to other pages on that site to get some data.

With my python app I was using mechanize for automatic cookie management. Is there something similar for Objective C that is portable to the iPhone?

Thanks for any help.

like image 234
dan Avatar asked Jan 13 '10 00:01

dan


People also ask

How do I change website cookies on iPhone?

Enabling cookies in Safari for iOS (iPhone/iPad/iPod touch)From your home screen, go to Settings > Safari. Make sure "Block All Cookies" is turned off. Once this is set, you can browse OverDrive websites normally.

Can you clear cookies for just one website on iPhone?

While in the Website Data screen, you can also choose to swipe to the left on individual site names and then tap on “Delete” from there to delete specific website data and cookies too. This process is the same for all iPhone, iPad, and iPod touch devices, regardless of what system software version they are running.

Is it good to block all cookies on iPhone?

And some privacy advocates recommend blocking cookies entirely, so that websites can't glean personal information about you. That said, while occasionally clearing cookies can be beneficial, we recommend leaving your cookies enabled because blocking them leads to an inconvenient and unsatisfying web experience.


2 Answers

NSURLConnection gives you cookie management for free. From the URL Loading System Programming Guide:

The URL loading system automatically sends any stored cookies appropriate for an NSURLRequest. unless the request specifies not to send cookies. Likewise, cookies returned in an NSURLResponse are accepted in accordance with the current cookie acceptance policy.

like image 106
Steve Madsen Avatar answered Sep 19 '22 03:09

Steve Madsen


You can use the NSURLConnection class to perform a HTTP request to login the website, and retrieve the cookie. To perform a request, just create an instance of NSURLConnection and assign a delegate object to it.

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]]; NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; 

Then, implement a delegate method.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;     NSDictionary *fields = [httpResponse allHeaderFields];     NSString *cookie = [fields valueForKey:@"Set-Cookie"]; // It is your cookie } 

Retain or copy the cookie string. When you want to perform another request, add it to your HTTP header of your NSURLRequest instance.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]]; [request addValue:cookie forHTTPHeaderField:@"Cookie"]; 
like image 42
zonble Avatar answered Sep 21 '22 03:09

zonble