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 6 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
   BOOL _savedHideKeyboardAccessoryView;
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
  * See https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/WebKitAvail.html.
46
  * See https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/WebKitAvail.html.
131
     [self addSubview:_webView];
126
     [self addSubview:_webView];
132
     [self setHideKeyboardAccessoryView: _savedHideKeyboardAccessoryView];
127
     [self setHideKeyboardAccessoryView: _savedHideKeyboardAccessoryView];
133
     [self visitSource];
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
 -(void)keyboardWillHide
144
 -(void)keyboardWillHide
140
 {
145
 {
141
     keyboardTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false];
146
     keyboardTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(keyboardDisplacementFix) userInfo:nil repeats:false];
142
     [[NSRunLoop mainRunLoop] addTimer:keyboardTimer forMode:NSRunLoopCommonModes];
147
     [[NSRunLoop mainRunLoop] addTimer:keyboardTimer forMode:NSRunLoopCommonModes];
143
 }
148
 }
149
+  
144
 -(void)keyboardWillShow
150
 -(void)keyboardWillShow
145
 {
151
 {
146
     if (keyboardTimer != nil) {
152
     if (keyboardTimer != nil) {
147
         [keyboardTimer invalidate];
153
         [keyboardTimer invalidate];
148
     }
154
     }
149
 }
155
 }
156
+
150
 -(void)keyboardDisplacementFix
157
 -(void)keyboardDisplacementFix
151
 {
158
 {
152
     // https://stackoverflow.com/a/9637807/824966
159
     // https://stackoverflow.com/a/9637807/824966