Wednesday, August 27, 2008

Add, Set, and Delete Cookies in UIWebView with NSHTTPCookieStorage

NSHTTPCookieStorage allows you to update the cookies for every request made from your application. UIWebViews will respond to this changes in cookies at run time and NSHTTPCookieStorage allows you to listen for changes in cookies as the are normaly set in the browser.

To get the cookies for any url use:
NSHTTPCookieStorage* cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* theCookies = [cookieStorage cookiesForURL:[NSURL URLWithString:@"http://example.com"]];


This makes it possible to easily login or logout a user from a website presented in your UIWebView.

Checkout NSHTTPCookieStorage* for all the details.

Labels: , , ,

3 Comments:

Blogger Matt Farnell said...

I'm having troubles with cookies although I need to spend more time on it.

have you tried the cache in UIWebView? when making requests the UIWebView seems to call cachedResponseForRequest from the cache before it makes the request but does not call storeCachedResponse when it gets a response, so effectively the cache is useless as it doesnt work. Have you looked into it or have you got any ideas? Thanks

September 25, 2008 1:10 PM  
Blogger Jay said...

Hello

I have a UIWebview added to my screen. After a certain time, I need to refresh the UIWebview with New URL.

Any idea how I can achieve that?

Thanks

October 6, 2008 11:54 AM  
Anonymous gh3 said...

Hi Andrew,

'm writing you due to the fact that i'm starting to play with iPhone sdk and in particular with UiWebView and I'm experencing some troubles with cookies.

I've had a look at your snippet, but i haven't understood how to manage it in order to use it with my applicationDidFinishLaunching.

Any tips for me?

October 13, 2008 11:13 AM  

Post a Comment

<< Home