How to Clean Web Cookies in UIWebView

Many websites create cookies. Especially the ones with login feature, to store the login session information. So if you are using Web View in your app for any such purpose. You may need to delete cookies at times.

Here is the code to clean cookies of a UIWebView –

1
2
3
4
5
6
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies]){
    [storage deleteCookie:cookie];
}
[[NSUserDefaults standardUserDefaults] synchronize];

This goes through all the cookies one by one and clears it.

Imp: Be sure to add the last NSUserDefaults line. Or the cookies would be back as you restart the app.

This is the best I could find. If you know of any other way, please let me know.

Namit Gupta

iOS Consultant at @RapidCircle. Passionate about Mobile Apps. Loves to Design. You can reach him on his social profiles below.

Comments 1

Leave a Reply