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
This commit is contained in:
Neil 2018-11-20 08:48:26 +08:00 committed by Thibault Malbranche
parent 692961bfca
commit 2b9292ed1c
1 changed files with 15 additions and 8 deletions

View File

@ -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