Want to clear cache from Reaact-native-webView in React Native, {CookieManager.clearAll();}-Not Working in iOS
Asked
Active
Viewed 1.1k times
5
-
So what is your code? – harmonica141 Jul 23 '19 at 07:32
-
componentDidMount() { CookieManager.clearAll() .then((res) => { console.log('CookieManager.clearAll =>', res); }); } – arun alexander arun alexander Jul 23 '19 at 08:58
-
– arun alexander arun alexander Jul 23 '19 at 08:59
-
Mind putting that in context and editing it into your question?! It is quite hard to understand what you are asking. – harmonica141 Jul 23 '19 at 09:19
-
I need to clear the cache from webView when every time I launch the WebView – arun alexander arun alexander Jul 23 '19 at 09:55
2 Answers
16
You can use the Incognito property in WebView for clearing the cache while you want to lunch the WebView.
<WebView
........
incognito={true}
/>

Y sharifpour
- 361
- 4
- 12
-
1This solution is better than others. As huent-1493 says, `incognito` property works both on ios and android while `cacheMode` and `cacheEnabled` works only for android https://github.com/react-native-webview/react-native-webview/issues/880#issuecomment-812305097 – khandaniel May 31 '22 at 10:28
4
I had to clear the cache on logout, had to create a native module, and bridge it to React Native. This is the code:
// ClearWebviewCache.m
#import "ClearWebviewCache.h"
#import "WebKit/WKWebsiteDataStore.h"
@implementation ClearWebviewCache
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(clearWebviewIOS:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject){
NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
dispatch_async(dispatch_get_main_queue(), ^{
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
return resolve(@"ok");
}];
});
}
@end
Header:
#import <React/RCTBridgeModule.h>
@interface ClearWebviewCache : NSObject <RCTBridgeModule>
@end
and you can then call this in React Native:
await NativeModules.ClearWebviewCache.clearWebviewIOS();

Anita
- 2,741
- 27
- 28