Skip to content

Commit

Permalink
feat: Add clearCache method on iOS (react-native-webview#3119)
Browse files Browse the repository at this point in the history
* Add clearCache method on iOS

* Update ClearData.tsx to remove comments

Co-authored-by: Jamon Holmgren <jamonholmgren@gmail.com>

* Update ClearData.tsx to remove commented code

Co-authored-by: Jamon Holmgren <jamonholmgren@gmail.com>

---------

Co-authored-by: Jamon Holmgren <jamonholmgren@gmail.com>
  • Loading branch information
robinheinze and jamonholmgren committed Aug 29, 2023
1 parent 17d2ffb commit 0868f91
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 54 deletions.
1 change: 1 addition & 0 deletions apple/RNCWebViewImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
- (void)reload;
- (void)stopLoading;
- (void)requestFocus;
- (void)clearCache:(BOOL)includeDiskFiles;
#ifdef RCT_NEW_ARCH_ENABLED
- (void)destroyWebView;
#endif
Expand Down
31 changes: 31 additions & 0 deletions apple/RNCWebViewImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,37 @@ - (void)requestFocus
#endif // !TARGET_OS_OSX
}

- (void)clearCache:(BOOL)includeDiskFiles
{
NSMutableSet *dataTypes = [NSMutableSet setWithArray:@[
WKWebsiteDataTypeMemoryCache,
WKWebsiteDataTypeOfflineWebApplicationCache,
]];
if (@available(iOS 11.3, *)) {
[dataTypes addObject:WKWebsiteDataTypeFetchCache];
}
if (includeDiskFiles) {
[dataTypes addObjectsFromArray:@[
WKWebsiteDataTypeDiskCache,
WKWebsiteDataTypeSessionStorage,
WKWebsiteDataTypeLocalStorage,
WKWebsiteDataTypeWebSQLDatabases,
WKWebsiteDataTypeIndexedDBDatabases
]];
}
[self removeData:dataTypes];
}

- (void)removeData:(NSSet *)dataTypes
{
if (_webView == nil) {
return;
}
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];

[_webView.configuration.websiteDataStore removeDataOfTypes:dataTypes modifiedSince:dateFrom completionHandler:^{}];
}

#if !TARGET_OS_OSX
- (void)setBounces:(BOOL)bounces
{
Expand Down
1 change: 1 addition & 0 deletions apple/RNCWebViewManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ - (RNCView *)view

QUICK_RCT_EXPORT_COMMAND_METHOD_PARAMS(postMessage, message:(NSString *)message, message)
QUICK_RCT_EXPORT_COMMAND_METHOD_PARAMS(injectJavaScript, script:(NSString *)script, script)
QUICK_RCT_EXPORT_COMMAND_METHOD_PARAMS(clearCache, includeDiskFiles:(BOOL)includeDiskFiles, includeDiskFiles)

RCT_EXPORT_METHOD(shouldStartLoadWithLockIdentifier:(BOOL)shouldStart
lockIdentifier:(double)lockIdentifier)
Expand Down
6 changes: 3 additions & 3 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1724,14 +1724,14 @@ Removes the autocomplete popup from the currently focused form field, if present

### `clearCache(bool)`[](#methods-index)

(android only)

```javascript
clearCache(true)
clearCache(true);
```

Clears the resource cache. Note that the cache is per-application, so this will clear the cache for all WebViews used. [developer.android.com reference](<https://developer.android.com/reference/android/webkit/WebView.html#clearCache(boolean)>)

In iOS, includeDiskFiles will also remove data from the web storages and databases.[developer.apple.com reference](https://developer.apple.com/documentation/webkit/wkwebsitedatastore/1532936-removedata)

### `clearHistory()`[](#methods-index)

(android only)
Expand Down
14 changes: 14 additions & 0 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import ApplePay from './examples/ApplePay';
import CustomMenu from './examples/CustomMenu';
import OpenWindow from './examples/OpenWindow';
import SuppressMenuItems from './examples/Suppress';
import ClearData from './examples/ClearData';

const TESTS = {
Messaging: {
Expand Down Expand Up @@ -57,6 +58,14 @@ const TESTS = {
return <Background />;
},
},
ClearData: {
title: 'ClearData',
testId: 'clearData',
description: 'Clear data test',
render() {
return <ClearData />;
},
},
Downloads: {
title: 'Downloads',
testId: 'downloads',
Expand Down Expand Up @@ -236,6 +245,11 @@ export default class App extends Component<Props, State> {
title="SuppressMenuItems"
onPress={() => this._changeTest('SuppressMenuItems')}
/>
<Button
testID="testType_clearData"
title="ClearData"
onPress={() => this._changeTest('ClearData')}
/>
</View>

{restarting ? null : (
Expand Down
46 changes: 46 additions & 0 deletions example/examples/ClearData.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { Component } from 'react';
import { View, Button } from 'react-native';

import WebView from 'react-native-webview';

type Props = {};
type State = {};

export default class ClearData extends Component<Props, State> {
state = {};

constructor(props) {
super(props);
this.webView = React.createRef();
}

clearCacheAndReload = (includeDiskFiles: boolean) => {
this.webView.current.clearCache(includeDiskFiles);
this.webView.current.reload();
};

reload = () => {
this.webView.current.reload();
};

render() {
return (
<View style={{ height: 1000 }}>
<Button
title="Clear cache (diskFiles)"
onPress={() => this.clearCacheAndReload(true)}
/>
<Button
title="Clear cache (no diskFiles)"
onPress={() => this.clearCacheAndReload(false)}
/>
<Button title="Reload" onPress={this.reload} />
<WebView
ref={this.webView}
source={{ uri: 'https://www.theguardian.com/international' }}
incognito={false}
/>
</View>
);
}
}
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ PODS:
- React-jsinspector (0.71.12)
- React-logger (0.71.12):
- glog
- react-native-webview (13.3.1):
- react-native-webview (13.4.0):
- React-Core
- React-perflogger (0.71.12)
- React-RCTActionSheet (0.71.12):
Expand Down Expand Up @@ -467,7 +467,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: a78a0e415dc4b786a4308becf3e3bc6dbbc7b92e
React-jsinspector: ec4dcbfb1f4e72f04f826a0301eceee5fa7ca540
React-logger: 35538accacf2583693fbc3ee8b53e69a1776fcee
react-native-webview: c2b70afb1d910cdd8810375aecc6c2894e2ba061
react-native-webview: 64c9bf9646e7377240fb87d70f74556af6433143
React-perflogger: 75b0e25075c67565a830985f3c373e2eae5389e0
React-RCTActionSheet: a0c3e916b327e297d124d9ebe8b0c721840ee04d
React-RCTAnimation: 3da7025801d7bf0f8cfd94574d6278d5b82a8b88
Expand Down
5 changes: 3 additions & 2 deletions src/WebViewTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ type WebViewCommands =
| 'postMessage'
| 'injectJavaScript'
| 'loadUrl'
| 'requestFocus';
| 'requestFocus'
| 'clearCache';

type AndroidWebViewCommands = 'clearHistory' | 'clearCache' | 'clearFormData';
type AndroidWebViewCommands = 'clearHistory' | 'clearFormData';

interface RNCWebViewUIManager<Commands extends string> extends UIManagerStatic {
getViewManagerConfig: (name: string) => {
Expand Down
Loading

0 comments on commit 0868f91

Please sign in to comment.