Skip to content

Commit 4b0427b

Browse files
committed
feat: support async navigation decision handling
1 parent 8a11f80 commit 4b0427b

2 files changed

Lines changed: 15 additions & 16 deletions

File tree

apple/RNCWebView.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ typedef enum RNCWebViewPermissionGrantType : NSUInteger {
1919

2020
@class RNCWebView;
2121

22-
@protocol RNCWebViewDelegate <NSObject>
23-
24-
- (BOOL)webView:(RNCWebView *_Nonnull)webView
25-
shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *_Nonnull)request
26-
withCallback:(RCTDirectEventBlock _Nonnull)callback;
27-
28-
@end
29-
3022
@interface RNCWeakScriptMessageDelegate : NSObject<WKScriptMessageHandler>
3123

3224
@property (nonatomic, weak, nullable) id<WKScriptMessageHandler> scriptDelegate;
@@ -37,7 +29,6 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *_Nonnull)request
3729

3830
@interface RNCWebView : RCTView
3931

40-
@property (nonatomic, weak) id<RNCWebViewDelegate> _Nullable delegate;
4132
@property (nonatomic, copy) NSDictionary * _Nullable source;
4233
@property (nonatomic, assign) BOOL messagingEnabled;
4334
@property (nonatomic, copy) NSString * _Nullable injectedJavaScript;

apple/RNCWebView.m

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import <React/RCTConvert.h>
1010
#import <React/RCTAutoInsetsProtocol.h>
1111
#import "RNCWKProcessPoolManager.h"
12+
#import "RNCWebViewDecisionManager.h"
1213
#if !TARGET_OS_OSX
1314
#import <UIKit/UIKit.h>
1415
#else
@@ -1122,6 +1123,16 @@ - (void) webView:(WKWebView *)webView
11221123
};
11231124

11241125
if (_onShouldStartLoadWithRequest) {
1126+
int lockIdentifier = [[RNCWebViewDecisionManager getInstance] setDecisionHandler:^(BOOL shouldStart) {
1127+
dispatch_async(dispatch_get_main_queue(), ^{
1128+
if (!shouldStart) {
1129+
decisionHandler(WKNavigationActionPolicyCancel);
1130+
return;
1131+
}
1132+
allowNavigation();
1133+
});
1134+
}];
1135+
11251136
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
11261137
if (request.mainDocumentURL) {
11271138
[event addEntriesFromDictionary: @{
@@ -1131,14 +1142,11 @@ - (void) webView:(WKWebView *)webView
11311142
[event addEntriesFromDictionary: @{
11321143
@"url": (request.URL).absoluteString,
11331144
@"navigationType": navigationTypes[@(navigationType)],
1134-
@"isTopFrame": @(isTopFrame)
1145+
@"isTopFrame": @(isTopFrame),
1146+
@"lockIdentifier": @(lockIdentifier)
11351147
}];
1136-
if (![self.delegate webView:self
1137-
shouldStartLoadForRequest:event
1138-
withCallback:_onShouldStartLoadWithRequest]) {
1139-
decisionHandler(WKNavigationActionPolicyCancel);
1140-
return;
1141-
}
1148+
_onShouldStartLoadWithRequest(event);
1149+
return;
11421150
}
11431151

11441152
// Allow all navigation by default

0 commit comments

Comments
 (0)