5

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

2 Answers2

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
  • 1
    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 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