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
Decoding JSON Web Tokens in Swift
How to Apply Uiappearance Proxy Properties to Uilabel
How to Integrate Facebook Without Redirect Safari Browser in iOS App with Latest Fbsdk
Working with C APIs from Swift
Changing the Height of the Navigation Bar iOS Swift
Unable to Get the Access Token Through [Fbsdkaccesstoken Currentaccesstoken] in iPhone Sdk
Need Assistance Setting Two Buttons of Equal Width, Side by Side Autolayout
Kcfstreamerrordomainssl, -9802 When Connecting to a Server by Ip Address Through Https in iOS 9
Valid File Path for Archiverootobject and Unarchiverootobject
How to Create an Empty Application in Xcode Without Storyboard
How to Determine Which Apps Are Background and Which App Is Foreground on iOS by Application Id
Swiftui - Multiple Buttons in a List Row
How to Use Bluetooth Classic Instead of Le
Prompt When Trying to Dial a Phone Number Using Tel:// Scheme on iOS 10.3
Why Does Apple Recommend to Use Dispatch_Once for Implementing the Singleton Pattern Under Arc
Are Nslayoutconstraints Animatable
iOS Perform Action After Period of Inactivity (No User Interaction)