Browse Source

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
Neil 5 years ago
parent
commit
2b9292ed1c
1 changed files with 15 additions and 8 deletions
  1. 15
    8
      ios/RNCWKWebView.m

+ 15
- 8
ios/RNCWKWebView.m View File

@@ -40,12 +40,7 @@ static NSString *const MessageHanderName = @"ReactNative";
40 40
   BOOL _savedHideKeyboardAccessoryView;
41 41
 }
42 42
 
43
-- (void)dealloc
44
-{
45
-    if(_webView){
46
-        [_webView removeObserver:self forKeyPath:@"estimatedProgress"];
47
-    }
48
-}
43
+- (void)dealloc{}
49 44
 
50 45
 /**
51 46
  * See https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/WebKitAvail.html.
@@ -131,22 +126,34 @@ static NSString *const MessageHanderName = @"ReactNative";
131 126
     [self addSubview:_webView];
132 127
     [self setHideKeyboardAccessoryView: _savedHideKeyboardAccessoryView];
133 128
     [self visitSource];
134
-  } else {
135
-    [_webView.configuration.userContentController removeScriptMessageHandlerForName:MessageHanderName];
136 129
   }
137 130
 }
138 131
 
132
+- (void)removeFromSuperview
133
+{
134
+    if (_webView) {
135
+        [_webView.configuration.userContentController removeScriptMessageHandlerForName:MessageHanderName];
136
+        [_webView removeObserver:self forKeyPath:@"estimatedProgress"];
137
+        [_webView removeFromSuperview];
138
+        _webView = nil;
139
+    }
140
+    
141
+    [super removeFromSuperview];
142
+}
143
+
139 144
 -(void)keyboardWillHide
140 145
 {
141 146
     keyboardTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false];
142 147
     [[NSRunLoop mainRunLoop] addTimer:keyboardTimer forMode:NSRunLoopCommonModes];
143 148
 }
149
+  
144 150
 -(void)keyboardWillShow
145 151
 {
146 152
     if (keyboardTimer != nil) {
147 153
         [keyboardTimer invalidate];
148 154
     }
149 155
 }
156
+
150 157
 -(void)keyboardDisplacementFix
151 158
 {
152 159
     // https://stackoverflow.com/a/9637807/824966