6

Want to clear cache from Reaact-native-webView in React Native, {CookieManager.clearAll();}-Not Working in iOS

5
  • So what is your code? Commented Jul 23, 2019 at 7:32
  • componentDidMount() { CookieManager.clearAll() .then((res) => { console.log('CookieManager.clearAll =>', res); }); } Commented 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} /> Commented 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. Commented Jul 23, 2019 at 9:19
  • I need to clear the cache from webView when every time I launch the WebView Commented Jul 23, 2019 at 9:55

3 Answers 3

19

You can use the Incognito property in WebView for clearing the cache while you want to lunch the WebView.

<WebView ........ incognito={true} /> 
Sign up to request clarification or add additional context in comments.

1 Comment

This 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 github.com/react-native-webview/react-native-webview/issues/…
8

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

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.