Skip to content

Commit f40a3ba

Browse files
(iOS) Fixing memory leak
1 parent ed1a5a9 commit f40a3ba

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

ios/RCTWebViewAlternative.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
@property (nonatomic, copy) RCTDirectEventBlock _Nullable onLoad;
1010
@property (nonatomic, copy) RCTDirectEventBlock _Nullable onMessage;
1111

12+
- (nonnull instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;
13+
- (nonnull instancetype)initWithFrame:(CGRect)frame configuration:(nonnull WKWebViewConfiguration *)configuration NS_UNAVAILABLE;
14+
- (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder NS_UNAVAILABLE;
15+
1216
- (BOOL)scrollEnabled;
1317
- (void)setScrollEnabled:(BOOL)enabled;
1418

ios/RCTWebViewAlternative.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ - (id)inputAccessoryView {
4545

4646
@implementation RCTWebViewAlternative
4747

48+
- (instancetype)init {
49+
if ((self = [super init])) { // -[UIView init] calls initWithFrame:, -[WKWebView initWithFrame:] calls initWithFrame:configuration:
50+
[self.configuration.userContentController addScriptMessageHandler:self name:@"jsMessageHandler"];
51+
}
52+
return self;
53+
}
54+
55+
- (instancetype)initWithCoder:(NSCoder *)coder {
56+
@throw [NSException exceptionWithName:@"Unsupported method" reason:nil userInfo:nil];
57+
}
58+
4859
- (UIView *)contentView {
4960
UIView *contentView;
5061
[[self valueForKey:@"_contentView"] getValue:&contentView];
@@ -167,4 +178,14 @@ - (void)userContentController:(WKUserContentController *)userContentController d
167178
});
168179
}
169180

181+
#pragma mark - UIView
182+
183+
- (void)willMoveToSuperview:(UIView *)newSuperview {
184+
[super willMoveToSuperview:newSuperview];
185+
186+
if (newSuperview == nil) {
187+
[self.configuration.userContentController removeScriptMessageHandlerForName:@"jsMessageHandler"];
188+
}
189+
}
190+
170191
@end

ios/WebViewAlternative.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ @implementation WebViewAlternativeManager
1515
- (RCTWebViewAlternative *)view {
1616
RCTWebViewAlternative *view = [RCTWebViewAlternative new];
1717
view.navigationDelegate = self;
18-
[view.configuration.userContentController addScriptMessageHandler:view name:@"jsMessageHandler"];
1918
return view;
2019
}
2120

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-webview-alternative",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Alternative to react-native-webview",
55
"main": "lib/commonjs/index.js",
66
"module": "lib/module/index.js",

0 commit comments

Comments
 (0)