Skip to content

Commit

Permalink
Add a removeData function
Browse files Browse the repository at this point in the history
This function will clear the specified data from the WKWebView. You
call the function with an array of the types you want to remove:

this.webView.removeData([
  WebsiteDataTypes.DiskCache,
  WebsiteDataTypes.OfflineWebApplicationCache,
  WebsiteDataTypes.MemoryCache,
  WebsiteDataTypes.LocalStorage,
  WebsiteDataTypes.Cookies,
  WebsiteDataTypes.SessionStorage,
  WebsiteDataTypes.IndexedDBDatabases,
  WebsiteDataTypes.WebSQLDatabases,
])

WebsiteDataTypes is now exported from the module, so you include:

import WKWebView, { WebsiteDataTypes } from 'react-native-wkwebview-reborn'

If you pass null for the array then all data types are removed.
  • Loading branch information
markdaws committed Apr 26, 2017
1 parent 1d784ae commit 29bd301
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
16 changes: 16 additions & 0 deletions WKWebView.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ var WKWebView = React.createClass({
return WKWebViewManager.evaluateJavaScript(this.getWebViewHandle(), js);
},

removeData: function(types) {
return WKWebViewManager.removeData(this.getWebViewHandle(), types)
},

/**
* We return an event with a bunch of fields including:
* url, title, loading, canGoBack, canGoForward
Expand Down Expand Up @@ -446,3 +450,15 @@ var styles = StyleSheet.create({
});

export default WKWebView;

const WebsiteDataTypes = {
DiskCache: 'WKWebsiteDataTypeDiskCache',
OfflineWebApplicationCache: 'WKWebsiteDataTypeOfflineWebApplicationCache',
MemoryCache: 'WKWebsiteDataTypeMemoryCache',
LocalStorage: 'WKWebsiteDataTypeLocalStorage',
Cookies: 'WKWebsiteDataTypeCookies',
SessionStorage: 'WKWebsiteDataTypeSessionStorage',
IndexedDBDatabases: 'WKWebsiteDataTypeIndexedDBDatabases',
WebSQLDatabases: 'WKWebsiteDataTypeWebSQLDatabases',
}
export { WebsiteDataTypes }
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

import WKWebView from './WKWebView';
import WKWebView, {WebsiteDataTypes} from './WKWebView';

export default WKWebView;
export { WebsiteDataTypes }
23 changes: 12 additions & 11 deletions ios/RCTWKWebView/RCTWKWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@ extern NSString *const RCTJSNavigationScheme;
@protocol RCTWKWebViewDelegate <NSObject>

- (BOOL)webView:(RCTWKWebView *)webView
shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
withCallback:(RCTDirectEventBlock)callback;
shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
withCallback:(RCTDirectEventBlock)callback;

@end

@interface RCTWKWebView : RCTView

@property (nonatomic, weak) id<RCTWKWebViewDelegate> delegate;

@property (nonatomic, copy) NSDictionary *source;
@property (nonatomic, assign) UIEdgeInsets contentInset;
@property (nonatomic, assign) BOOL automaticallyAdjustContentInsets;
@property (nonatomic, assign) BOOL openNewWindowInWebView;
@property (nonatomic, copy) NSString *injectedJavaScript;
@property(nonatomic, weak) id<RCTWKWebViewDelegate> delegate;

@property(nonatomic, copy) NSDictionary *source;
@property(nonatomic, assign) UIEdgeInsets contentInset;
@property(nonatomic, assign) BOOL automaticallyAdjustContentInsets;
@property(nonatomic, assign) BOOL openNewWindowInWebView;
@property(nonatomic, copy) NSString *injectedJavaScript;

- (void)goForward;
- (void)goBack;
- (void)reload;
- (void)stopLoading;
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(id, NSError *error))completionHandler;

- (void)evaluateJavaScript:(NSString *)javaScriptString
completionHandler:(void (^)(id, NSError *error))completionHandler;
-(void)removeData:(NSArray *)types
completionHandler:(void (^)(id, NSError *error))completionHandler;
@end
17 changes: 17 additions & 0 deletions ios/RCTWKWebView/RCTWKWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ - (void)evaluateJavaScript:(NSString *)javaScriptString
[_webView evaluateJavaScript:javaScriptString completionHandler:completionHandler];
}

- (void)removeData:(NSArray *)types
completionHandler:(void (^)(id, NSError *error))completionHandler
{
NSSet *websiteDataTypes;

if (types != nil) {
websiteDataTypes = [NSSet setWithArray:types];
} else {
websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
}

NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
completionHandler(self, nil);
}];
}

- (void)goBack
{
[_webView goBack];
Expand Down
22 changes: 22 additions & 0 deletions ios/RCTWKWebView/RCTWKWebViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ - (UIView *)view
}];
}

RCT_EXPORT_METHOD(removeData:(nonnull NSNumber *)reactTag
types:(NSArray *)types
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTWKWebView *> *viewRegistry) {
RCTWKWebView *view = viewRegistry[reactTag];
if (![view isKindOfClass:[RCTWKWebView class]]) {
RCTLogError(@"Invalid view returned from registry, expecting RCTWKWebView, got: %@", view);
} else {
[view removeData:types completionHandler:^(id result, NSError *error) {
if (error) {
reject(@"removeFailed", @"Error calling removeData", error);
} else {
resolve(nil);
}
}];
}
}];

}

#pragma mark - Exported synchronous methods

- (BOOL)webView:(__unused RCTWKWebView *)webView
Expand Down

0 comments on commit 29bd301

Please sign in to comment.