Skip to content

Commit 0868f91

Browse files
robinheinzejamonholmgren
andauthoredAug 29, 2023
feat: Add clearCache method on iOS (#3119)
* 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>
1 parent 17d2ffb commit 0868f91

File tree

9 files changed

+129
-54
lines changed

9 files changed

+129
-54
lines changed
 

Diff for: ‎apple/RNCWebViewImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
136136
- (void)reload;
137137
- (void)stopLoading;
138138
- (void)requestFocus;
139+
- (void)clearCache:(BOOL)includeDiskFiles;
139140
#ifdef RCT_NEW_ARCH_ENABLED
140141
- (void)destroyWebView;
141142
#endif

Diff for: ‎apple/RNCWebViewImpl.m

+31
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,37 @@ - (void)requestFocus
15701570
#endif // !TARGET_OS_OSX
15711571
}
15721572

1573+
- (void)clearCache:(BOOL)includeDiskFiles
1574+
{
1575+
NSMutableSet *dataTypes = [NSMutableSet setWithArray:@[
1576+
WKWebsiteDataTypeMemoryCache,
1577+
WKWebsiteDataTypeOfflineWebApplicationCache,
1578+
]];
1579+
if (@available(iOS 11.3, *)) {
1580+
[dataTypes addObject:WKWebsiteDataTypeFetchCache];
1581+
}
1582+
if (includeDiskFiles) {
1583+
[dataTypes addObjectsFromArray:@[
1584+
WKWebsiteDataTypeDiskCache,
1585+
WKWebsiteDataTypeSessionStorage,
1586+
WKWebsiteDataTypeLocalStorage,
1587+
WKWebsiteDataTypeWebSQLDatabases,
1588+
WKWebsiteDataTypeIndexedDBDatabases
1589+
]];
1590+
}
1591+
[self removeData:dataTypes];
1592+
}
1593+
1594+
- (void)removeData:(NSSet *)dataTypes
1595+
{
1596+
if (_webView == nil) {
1597+
return;
1598+
}
1599+
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
1600+
1601+
[_webView.configuration.websiteDataStore removeDataOfTypes:dataTypes modifiedSince:dateFrom completionHandler:^{}];
1602+
}
1603+
15731604
#if !TARGET_OS_OSX
15741605
- (void)setBounces:(BOOL)bounces
15751606
{

Diff for: ‎apple/RNCWebViewManager.mm

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ - (RNCView *)view
213213

214214
QUICK_RCT_EXPORT_COMMAND_METHOD_PARAMS(postMessage, message:(NSString *)message, message)
215215
QUICK_RCT_EXPORT_COMMAND_METHOD_PARAMS(injectJavaScript, script:(NSString *)script, script)
216+
QUICK_RCT_EXPORT_COMMAND_METHOD_PARAMS(clearCache, includeDiskFiles:(BOOL)includeDiskFiles, includeDiskFiles)
216217

217218
RCT_EXPORT_METHOD(shouldStartLoadWithLockIdentifier:(BOOL)shouldStart
218219
lockIdentifier:(double)lockIdentifier)

Diff for: ‎docs/Reference.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1724,14 +1724,14 @@ Removes the autocomplete popup from the currently focused form field, if present
17241724

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

1727-
(android only)
1728-
17291727
```javascript
1730-
clearCache(true)
1728+
clearCache(true);
17311729
```
17321730

17331731
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)>)
17341732

1733+
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)
1734+
17351735
### `clearHistory()`[](#methods-index)
17361736

17371737
(android only)

Diff for: ‎example/App.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import ApplePay from './examples/ApplePay';
2323
import CustomMenu from './examples/CustomMenu';
2424
import OpenWindow from './examples/OpenWindow';
2525
import SuppressMenuItems from './examples/Suppress';
26+
import ClearData from './examples/ClearData';
2627

2728
const TESTS = {
2829
Messaging: {
@@ -57,6 +58,14 @@ const TESTS = {
5758
return <Background />;
5859
},
5960
},
61+
ClearData: {
62+
title: 'ClearData',
63+
testId: 'clearData',
64+
description: 'Clear data test',
65+
render() {
66+
return <ClearData />;
67+
},
68+
},
6069
Downloads: {
6170
title: 'Downloads',
6271
testId: 'downloads',
@@ -236,6 +245,11 @@ export default class App extends Component<Props, State> {
236245
title="SuppressMenuItems"
237246
onPress={() => this._changeTest('SuppressMenuItems')}
238247
/>
248+
<Button
249+
testID="testType_clearData"
250+
title="ClearData"
251+
onPress={() => this._changeTest('ClearData')}
252+
/>
239253
</View>
240254

241255
{restarting ? null : (

Diff for: ‎example/examples/ClearData.tsx

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React, { Component } from 'react';
2+
import { View, Button } from 'react-native';
3+
4+
import WebView from 'react-native-webview';
5+
6+
type Props = {};
7+
type State = {};
8+
9+
export default class ClearData extends Component<Props, State> {
10+
state = {};
11+
12+
constructor(props) {
13+
super(props);
14+
this.webView = React.createRef();
15+
}
16+
17+
clearCacheAndReload = (includeDiskFiles: boolean) => {
18+
this.webView.current.clearCache(includeDiskFiles);
19+
this.webView.current.reload();
20+
};
21+
22+
reload = () => {
23+
this.webView.current.reload();
24+
};
25+
26+
render() {
27+
return (
28+
<View style={{ height: 1000 }}>
29+
<Button
30+
title="Clear cache (diskFiles)"
31+
onPress={() => this.clearCacheAndReload(true)}
32+
/>
33+
<Button
34+
title="Clear cache (no diskFiles)"
35+
onPress={() => this.clearCacheAndReload(false)}
36+
/>
37+
<Button title="Reload" onPress={this.reload} />
38+
<WebView
39+
ref={this.webView}
40+
source={{ uri: 'https://www.theguardian.com/international' }}
41+
incognito={false}
42+
/>
43+
</View>
44+
);
45+
}
46+
}

Diff for: ‎example/ios/Podfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ PODS:
233233
- React-jsinspector (0.71.12)
234234
- React-logger (0.71.12):
235235
- glog
236-
- react-native-webview (13.3.1):
236+
- react-native-webview (13.4.0):
237237
- React-Core
238238
- React-perflogger (0.71.12)
239239
- React-RCTActionSheet (0.71.12):
@@ -467,7 +467,7 @@ SPEC CHECKSUMS:
467467
React-jsiexecutor: a78a0e415dc4b786a4308becf3e3bc6dbbc7b92e
468468
React-jsinspector: ec4dcbfb1f4e72f04f826a0301eceee5fa7ca540
469469
React-logger: 35538accacf2583693fbc3ee8b53e69a1776fcee
470-
react-native-webview: c2b70afb1d910cdd8810375aecc6c2894e2ba061
470+
react-native-webview: 64c9bf9646e7377240fb87d70f74556af6433143
471471
React-perflogger: 75b0e25075c67565a830985f3c373e2eae5389e0
472472
React-RCTActionSheet: a0c3e916b327e297d124d9ebe8b0c721840ee04d
473473
React-RCTAnimation: 3da7025801d7bf0f8cfd94574d6278d5b82a8b88

Diff for: ‎src/WebViewTypes.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ type WebViewCommands =
2121
| 'postMessage'
2222
| 'injectJavaScript'
2323
| 'loadUrl'
24-
| 'requestFocus';
24+
| 'requestFocus'
25+
| 'clearCache';
2526

26-
type AndroidWebViewCommands = 'clearHistory' | 'clearCache' | 'clearFormData';
27+
type AndroidWebViewCommands = 'clearHistory' | 'clearFormData';
2728

2829
interface RNCWebViewUIManager<Commands extends string> extends UIManagerStatic {
2930
getViewManagerConfig: (name: string) => {

Diff for: ‎yarn.lock

+28-47
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.