fix(iOS): WKWebView RetainCycle (#1096)

This commit is contained in:
sunzhongliang 2019-12-27 18:39:53 +08:00 committed by Thibault Malbranche
parent 7ba1bc5129
commit 4f4644ffd8
2 changed files with 26 additions and 2 deletions

View File

@ -19,6 +19,11 @@
@end
@interface RNCWeakScriptMessageDelegate : NSObject<WKScriptMessageHandler>
@property (nonatomic, weak) id<WKScriptMessageHandler> scriptDelegate;
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate;
@end
@interface RNCWebView : RCTView
@property (nonatomic, weak) id<RNCWebViewDelegate> _Nullable delegate;

View File

@ -162,7 +162,8 @@ static NSDictionary* customCertificatesForHost;
wkWebViewConfig.userContentController = [WKUserContentController new];
// Shim the HTML5 history API:
[wkWebViewConfig.userContentController addScriptMessageHandler:self name:HistoryShimName];
[wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
name:HistoryShimName];
NSString *source = [NSString stringWithFormat:
@"(function(history) {\n"
" function notify(type) {\n"
@ -187,7 +188,8 @@ static NSDictionary* customCertificatesForHost;
[wkWebViewConfig.userContentController addUserScript:script];
if (_messagingEnabled) {
[wkWebViewConfig.userContentController addScriptMessageHandler:self name:MessageHandlerName];
[wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
name:MessageHandlerName];
NSString *source = [NSString stringWithFormat:
@"window.%@ = {"
@ -1077,3 +1079,20 @@ static NSDictionary* customCertificatesForHost;
}
@end
@implementation RNCWeakScriptMessageDelegate
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate {
self = [super init];
if (self) {
_scriptDelegate = scriptDelegate;
}
return self;
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
[self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message];
}
@end