Want to clear cache from Reaact-native-webView in React Native, {CookieManager.clearAll();}-Not Working in iOS
- So what is your code?harmonica141– harmonica1412019-07-23 07:32:51 +00:00Commented Jul 23, 2019 at 7:32
- componentDidMount() { CookieManager.clearAll() .then((res) => { console.log('CookieManager.clearAll =>', res); }); }arun alexander– arun alexander2019-07-23 08:58:32 +00:00Commented Jul 23, 2019 at 8:58
- <WebView style={{ flex: 1 }} source={{ uri: this.props.navigation.state.params.datass }} onNavigationStateChange={this._onNavigationStateChange.bind(this)} renderLoading={this.ActivityIndicatorLoadingView} javaScriptEnabled={true} domStorageEnabled={true} useWebKit={true} startInLoadingState={true} />arun alexander– arun alexander2019-07-23 08:59:21 +00:00Commented Jul 23, 2019 at 8:59
- Mind putting that in context and editing it into your question?! It is quite hard to understand what you are asking.harmonica141– harmonica1412019-07-23 09:19:25 +00:00Commented Jul 23, 2019 at 9:19
- I need to clear the cache from webView when every time I launch the WebViewarun alexander– arun alexander2019-07-23 09:55:31 +00:00Commented Jul 23, 2019 at 9:55
3 Answers
You can use the Incognito property in WebView for clearing the cache while you want to lunch the WebView.
<WebView ........ incognito={true} /> 1 Comment
incognito property works both on ios and android while cacheMode and cacheEnabled works only for android github.com/react-native-webview/react-native-webview/issues/…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();
Comments
I use my own async function to clear the WebView cache. Here’s a simple approach that might help:
async function clearCache() { setIncognito(true); setWebKey(prevKey => prevKey + 1); ToastAndroid.show('Cache cleared', ToastAndroid.SHORT); await new Promise(resolve => setTimeout(resolve, 500)); setIncognito(false); } How it works:
First, I use useState to manage the incognito mode, initially set to false. When clearCache() is triggered, it enables incognito mode and reloads the WebView. After 500ms, it switches back to normal mode. You can adjust the timeout based on your needs. In my opinion, no casual user would perform actions that fast, so keeping the cache for a short moment can still be useful.