arun alexander
arun alexander

Reputation: 79

Want to clear cache from webView in React Native CookieManager.clearAll(); Not Working in iOS

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

Upvotes: 6

Views: 16789

Answers (2)

Anita
Anita

Reputation: 3146

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();

Upvotes: 7

Y sharifpour
Y sharifpour

Reputation: 389

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

<WebView
........
incognito={true}
/>

Upvotes: 18

Related Questions