Skip to content

Commit 3793ee0

Browse files
committed
Added url blocking based on prefix
1 parent 45705c3 commit 3793ee0

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

AQWebView.ios.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ WKWebView.propTypes = {
3535
bottom: PropTypes.number,
3636
right: PropTypes.number
3737
}),
38+
39+
blockedPrefixes: PropTypes.arrayOf(PropTypes.string),
40+
onPrefixBlocked: PropTypes.func,
3841
};
3942

4043
var AQWebView = requireNativeComponent('AQWebView', WKWebView);

AQWebView/AQWebView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
@property (nonatomic, assign) UIEdgeInsets contentInset;
66
@property (nonatomic, assign) BOOL automaticallyAdjustContentInsets;
77
@property (nonatomic, copy) NSDictionary *source;
8+
@property (nonatomic, copy) NSArray<NSString*> *blockedPrefixes;
89

910
- (void)reload;
1011

AQWebView/AQWebView.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ @interface AQWebView () <RCTAutoInsetsProtocol, WKNavigationDelegate>
1010
@property (nonatomic, copy) RCTDirectEventBlock onLoadingStart;
1111
@property (nonatomic, copy) RCTDirectEventBlock onLoadingFinish;
1212
@property (nonatomic, copy) RCTDirectEventBlock onLoadingError;
13+
@property (nonatomic, copy) RCTDirectEventBlock onPrefixBlocked;
14+
1315
@end
1416

1517
@implementation AQWebView
@@ -147,6 +149,35 @@ - (void)refreshContentInset
147149

148150
#pragma mark - WKNavigationDelegate
149151

152+
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
153+
{
154+
NSURL *url = navigationAction.request.URL;
155+
NSString *urlStr = [url absoluteString];
156+
BOOL block = false;
157+
158+
if ([urlStr isEqual: @""]) {
159+
urlStr = @"about:blank";
160+
}
161+
162+
for (id prefix in _blockedPrefixes) {
163+
if ([urlStr hasPrefix:prefix]) {
164+
NSMutableDictionary<NSString *, id> *event = [[NSMutableDictionary alloc] initWithDictionary:
165+
@{
166+
@"url": urlStr,
167+
}
168+
];
169+
_onPrefixBlocked(event);
170+
decisionHandler(WKNavigationActionPolicyCancel);
171+
block = true;
172+
break;
173+
}
174+
}
175+
176+
if (!block) {
177+
decisionHandler(WKNavigationActionPolicyAllow);
178+
}
179+
}
180+
150181
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
151182
{
152183
[_spinner stopAnimating];

AQWebView/AQWebViewManager.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ - (UIView *) view
2222
RCT_EXPORT_VIEW_PROPERTY(onLoadingFinish, RCTDirectEventBlock)
2323
RCT_EXPORT_VIEW_PROPERTY(onLoadingError, RCTDirectEventBlock)
2424
RCT_EXPORT_VIEW_PROPERTY(source, NSDictionary)
25+
RCT_EXPORT_VIEW_PROPERTY(blockedPrefixes, NSArray<NSString*>)
26+
RCT_EXPORT_VIEW_PROPERTY(onPrefixBlocked, RCTDirectEventBlock)
2527

2628
RCT_EXPORT_METHOD(reload:(nonnull NSNumber *)reactTag)
2729
{

0 commit comments

Comments
 (0)