From b3e753690a97165fc844268e60b3d99089c6ec83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20M=C3=BCller?= Date: Sun, 26 Jan 2020 14:47:27 +0100 Subject: [PATCH] fix(iOS): `clearAll(true)` method with `useWebKit` enabled --- ios/RNCookieManagerIOS/RNCookieManagerIOS.m | 34 ++++++++------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/ios/RNCookieManagerIOS/RNCookieManagerIOS.m b/ios/RNCookieManagerIOS/RNCookieManagerIOS.m index 253c0b9..641a0fc 100644 --- a/ios/RNCookieManagerIOS/RNCookieManagerIOS.m +++ b/ios/RNCookieManagerIOS/RNCookieManagerIOS.m @@ -1,8 +1,8 @@ -/** - * Copyright (c) Joseph P. Ferraro - * - * This source code is licensed under the MIT license found in the - * LICENSE file here: https://github.com/joeferraro/react-native-cookies/blob/master/LICENSE.md. +/** + * Copyright (c) Joseph P. Ferraro + * + * This source code is licensed under the MIT license found in the + * LICENSE file here: https://github.com/joeferraro/react-native-cookies/blob/master/LICENSE.md. */ #import "RNCookieManagerIOS.h" @@ -181,22 +181,14 @@ -(NSString *)getDomainName:(NSURL *) url if (useWebKit) { if (@available(iOS 11.0, *)) { dispatch_async(dispatch_get_main_queue(), ^(){ - WKHTTPCookieStore *cookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore]; - [cookieStore getAllCookies:^(NSArray *allCookies) { - for(NSHTTPCookie *currentCookie in allCookies) { - // Uses the NSHTTPCookie directly has no effect, nor deleted the cookie nor thrown an error. - // Create a new cookie with the given values and delete this one do the work. - NSMutableDictionary *cookieData = [NSMutableDictionary dictionary]; - [cookieData setValue:currentCookie.name forKey:NSHTTPCookieName]; - [cookieData setValue:currentCookie.value forKey:NSHTTPCookieValue]; - [cookieData setValue:currentCookie.domain forKey:NSHTTPCookieDomain]; - [cookieData setValue:currentCookie.path forKey:NSHTTPCookiePath]; - - NSHTTPCookie *newCookie = [NSHTTPCookie cookieWithProperties:cookieData]; - [cookieStore deleteCookie:newCookie completionHandler:^{}]; - } - resolve(nil); - }]; + // https://stackoverflow.com/questions/46465070/how-to-delete-cookies-from-wkhttpcookiestore#answer-47928399 + NSSet *websiteDataTypes = [NSSet setWithArray:@[WKWebsiteDataTypeCookies]]; + NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0]; + [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes + modifiedSince:dateFrom + completionHandler:^() { + resolve(nil); + }]; }); } else { reject(@"", NOT_AVAILABLE_ERROR_MESSAGE, nil);