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 –

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

Technical Lead iOS @ONEChampionship | Ex-SDE2 @ Halodoc, Ex-iOS Lead @ Vokal. ❤️ Swift, designing pixel-perfect UI and writing clean code. Check my portfolio here.

Comments 1

Leave a Reply