How to Delete All Cookies of Uiwebview

How to delete all cookies of UIWebView?

According to this question, you can go through each cookie in the "Cookie Jar" and delete them, like so:

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies]) {
[storage deleteCookie:cookie];
}
[[NSUserDefaults standardUserDefaults] synchronize];

How to delete cookies from WKHttpCookieStore?

Update: Previously I filled a bugreport at bugreport.apple.com and now I can confirm this is fixed in iOS 12.


I jumped to conclusion that it's an iOS bug. So, instead of deleting cookies now I'm replacing them with cookies with an empty value:

NSDictionary properties = NSDictionary.FromObjectsAndKeys(
objects: new NSObject[]
{
new NSString(cookieToDelete.Name ?? ""),
new NSString(""),
new NSString(cookieToDelete.Path ?? ""),
new NSString(cookieToDelete.Domain ?? ""),
},
keys: new NSObject[]
{
NSHttpCookie.KeyName,
NSHttpCookie.KeyValue,
NSHttpCookie.KeyPath,
NSHttpCookie.KeyDomain,
}
);
NSHttpCookie cookieToReplace = NSHttpCookie.CookieFromProperties(properties);
await cookieStore.SetCookieAsync(cookieToReplace);

Delete cookies UIWebView

Try something like this:

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:
[NSURL URLWithString:@"http://login.facebook.com"]];
for (NSHTTPCookie* cookie in facebookCookies) {
[cookies deleteCookie:cookie];
}

Clear webView cookies (Swift)

I made it by

        let cookieJar = NSHTTPCookieStorage.sharedHTTPCookieStorage()

for cookie in cookieJar.cookies! {
// print(cookie.name+"="+cookie.value)
cookieJar.deleteCookie(cookie)
}

Swift 4

func removeCookies(){
let cookieJar = HTTPCookieStorage.shared

for cookie in cookieJar.cookies! {
cookieJar.deleteCookie(cookie)
}
}

How to delete WKWebview cookies

Apple released new APIs for iOS 9, so now we can remove domain specific cookies stored for WKWebView with below code, but this will only work on devices with iOS version 9 or later:

Below is the Swift 3 version

let dataStore = WKWebsiteDataStore.default()
dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { (records) in
for record in records {
if record.displayName.contains("facebook") {
dataStore.removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), for: [record], completionHandler: {
print("Deleted: " + record.displayName);
})
}
}
}

And Swift 4:

let dataStore = WKWebsiteDataStore.default()
dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
dataStore.removeData(
ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(),
for: records.filter { $0.displayName.contains("facebook") },
completionHandler: completion
)
}

Objective-C version -

WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore];
[dateStore
fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes]
completionHandler:^(NSArray * __nonnull records) {
for (WKWebsiteDataRecord *record in records) {
if ( [record.displayName containsString:@"facebook"]) {
[[WKWebsiteDataStore defaultDataStore]
removeDataOfTypes:record.dataTypes
forDataRecords:@[record]
completionHandler:^{
NSLog(@"Cookies for %@ deleted successfully",record.displayName);
}
];
}
}
}
];

The above snippet will sure work for iOS 9 and later. Unfortunately, if we use WKWebView for iOS versions before iOS 9, we still have to stick to the traditional method and delete the whole cookies storage as below.

NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
NSError *errors;
[[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];

How to clear the Cookies in UIWebView

You can go through each cookie in the cookie jar and remove them.

How to delete all cookies of UIWebView?

NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
[storage deleteCookie:cookie];
}


Related Topics



Leave a reply



Submit