Skip to content

Commit 68d7620

Browse files
jacobmllr95safaiyeh
authored andcommitted
fix(iOS): clearAll(true) correctly clears WebKit cookies (#23)
1 parent 354bc56 commit 68d7620

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

ios/RNCookieManagerIOS/RNCookieManagerIOS.m

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/**
2-
* Copyright (c) Joseph P. Ferraro
3-
*
4-
* This source code is licensed under the MIT license found in the
5-
* LICENSE file here: https://github.com/joeferraro/react-native-cookies/blob/master/LICENSE.md.
1+
/**
2+
* Copyright (c) Joseph P. Ferraro
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file here: https://github.com/joeferraro/react-native-cookies/blob/master/LICENSE.md.
66
*/
77

88
#import "RNCookieManagerIOS.h"
@@ -181,22 +181,14 @@ -(NSString *)getDomainName:(NSURL *) url
181181
if (useWebKit) {
182182
if (@available(iOS 11.0, *)) {
183183
dispatch_async(dispatch_get_main_queue(), ^(){
184-
WKHTTPCookieStore *cookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore];
185-
[cookieStore getAllCookies:^(NSArray<NSHTTPCookie *> *allCookies) {
186-
for(NSHTTPCookie *currentCookie in allCookies) {
187-
// Uses the NSHTTPCookie directly has no effect, nor deleted the cookie nor thrown an error.
188-
// Create a new cookie with the given values and delete this one do the work.
189-
NSMutableDictionary<NSHTTPCookiePropertyKey, id> *cookieData = [NSMutableDictionary dictionary];
190-
[cookieData setValue:currentCookie.name forKey:NSHTTPCookieName];
191-
[cookieData setValue:currentCookie.value forKey:NSHTTPCookieValue];
192-
[cookieData setValue:currentCookie.domain forKey:NSHTTPCookieDomain];
193-
[cookieData setValue:currentCookie.path forKey:NSHTTPCookiePath];
194-
195-
NSHTTPCookie *newCookie = [NSHTTPCookie cookieWithProperties:cookieData];
196-
[cookieStore deleteCookie:newCookie completionHandler:^{}];
197-
}
198-
resolve(nil);
199-
}];
184+
// https://stackoverflow.com/questions/46465070/how-to-delete-cookies-from-wkhttpcookiestore#answer-47928399
185+
NSSet *websiteDataTypes = [NSSet setWithArray:@[WKWebsiteDataTypeCookies]];
186+
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
187+
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes
188+
modifiedSince:dateFrom
189+
completionHandler:^() {
190+
resolve(nil);
191+
}];
200192
});
201193
} else {
202194
reject(@"", NOT_AVAILABLE_ERROR_MESSAGE, nil);

0 commit comments

Comments
 (0)