From 2b9292ed1c4c339ac633cec5fbacd24c6bef4e18 Mon Sep 17 00:00:00 2001 From: Neil <812188608@qq.com> Date: Tue, 20 Nov 2018 08:48:26 +0800 Subject: [PATCH] fix(WKWebView): Fix the message handle bug. (#143) * fix webview always recreate * fix webview always recreate * code format * Update RNCWKWebView.m * @bugfix fix the message handle problem --- ios/RNCWKWebView.m | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/ios/RNCWKWebView.m b/ios/RNCWKWebView.m index cf0a311..7afe599 100644 --- a/ios/RNCWKWebView.m +++ b/ios/RNCWKWebView.m @@ -40,12 +40,7 @@ static NSString *const MessageHanderName = @"ReactNative"; BOOL _savedHideKeyboardAccessoryView; } -- (void)dealloc -{ - if(_webView){ - [_webView removeObserver:self forKeyPath:@"estimatedProgress"]; - } -} +- (void)dealloc{} /** * See https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/WebKitAvail.html. @@ -131,22 +126,34 @@ static NSString *const MessageHanderName = @"ReactNative"; [self addSubview:_webView]; [self setHideKeyboardAccessoryView: _savedHideKeyboardAccessoryView]; [self visitSource]; - } else { - [_webView.configuration.userContentController removeScriptMessageHandlerForName:MessageHanderName]; } } +- (void)removeFromSuperview +{ + if (_webView) { + [_webView.configuration.userContentController removeScriptMessageHandlerForName:MessageHanderName]; + [_webView removeObserver:self forKeyPath:@"estimatedProgress"]; + [_webView removeFromSuperview]; + _webView = nil; + } + + [super removeFromSuperview]; +} + -(void)keyboardWillHide { keyboardTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false]; [[NSRunLoop mainRunLoop] addTimer:keyboardTimer forMode:NSRunLoopCommonModes]; } + -(void)keyboardWillShow { if (keyboardTimer != nil) { [keyboardTimer invalidate]; } } + -(void)keyboardDisplacementFix { // https://stackoverflow.com/a/9637807/824966