Browse Source

fix(iOS): Trigger _onContentProcessDidTerminate when removing webview from superview (#1378 by @pmusaraj)

[skip ci]
Penar Musaraj 4 years ago
parent
commit
9240536afa
No account linked to committer's email address
1 changed files with 17 additions and 13 deletions
  1. 17
    13
      apple/RNCWebView.m

+ 17
- 13
apple/RNCWebView.m View File

146
     selector:@selector(appDidBecomeActive)
146
     selector:@selector(appDidBecomeActive)
147
         name:UIApplicationDidBecomeActiveNotification
147
         name:UIApplicationDidBecomeActiveNotification
148
       object:nil];
148
       object:nil];
149
-    
149
+
150
     [[NSNotificationCenter defaultCenter]addObserver:self
150
     [[NSNotificationCenter defaultCenter]addObserver:self
151
     selector:@selector(appWillResignActive)
151
     selector:@selector(appWillResignActive)
152
         name:UIApplicationWillResignActiveNotification
152
         name:UIApplicationWillResignActiveNotification
174
                                                selector:@selector(hideFullScreenVideoStatusBars)
174
                                                selector:@selector(hideFullScreenVideoStatusBars)
175
                                                    name:UIWindowDidBecomeHiddenNotification
175
                                                    name:UIWindowDidBecomeHiddenNotification
176
                                                  object:nil];
176
                                                  object:nil];
177
-      
177
+
178
   }
178
   }
179
 #endif // !TARGET_OS_OSX
179
 #endif // !TARGET_OS_OSX
180
   return self;
180
   return self;
242
   if (_applicationNameForUserAgent) {
242
   if (_applicationNameForUserAgent) {
243
       wkWebViewConfig.applicationNameForUserAgent = [NSString stringWithFormat:@"%@ %@", wkWebViewConfig.applicationNameForUserAgent, _applicationNameForUserAgent];
243
       wkWebViewConfig.applicationNameForUserAgent = [NSString stringWithFormat:@"%@ %@", wkWebViewConfig.applicationNameForUserAgent, _applicationNameForUserAgent];
244
   }
244
   }
245
-  
245
+
246
   return wkWebViewConfig;
246
   return wkWebViewConfig;
247
 }
247
 }
248
 
248
 
307
         _webView.scrollView.delegate = nil;
307
         _webView.scrollView.delegate = nil;
308
 #endif // !TARGET_OS_OSX
308
 #endif // !TARGET_OS_OSX
309
         _webView = nil;
309
         _webView = nil;
310
+        if (_onContentProcessDidTerminate) {
311
+          NSMutableDictionary<NSString *, id> *event = [self baseEvent];
312
+          _onContentProcessDidTerminate(event);
313
+        }
310
     }
314
     }
311
 
315
 
312
     [super removeFromSuperview];
316
     [super removeFromSuperview];
1102
   if (_ignoreSilentHardwareSwitch) {
1106
   if (_ignoreSilentHardwareSwitch) {
1103
     [self forceIgnoreSilentHardwareSwitch:true];
1107
     [self forceIgnoreSilentHardwareSwitch:true];
1104
   }
1108
   }
1105
-    
1109
+
1106
   if (_onLoadingFinish) {
1110
   if (_onLoadingFinish) {
1107
     _onLoadingFinish([self baseEvent]);
1111
     _onLoadingFinish([self baseEvent]);
1108
   }
1112
   }
1155
 
1159
 
1156
 - (void)setInjectedJavaScript:(NSString *)source {
1160
 - (void)setInjectedJavaScript:(NSString *)source {
1157
   _injectedJavaScript = source;
1161
   _injectedJavaScript = source;
1158
-  
1162
+
1159
   self.atEndScript = source == nil ? nil : [[WKUserScript alloc] initWithSource:source
1163
   self.atEndScript = source == nil ? nil : [[WKUserScript alloc] initWithSource:source
1160
       injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
1164
       injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
1161
     forMainFrameOnly:_injectedJavaScriptForMainFrameOnly];
1165
     forMainFrameOnly:_injectedJavaScriptForMainFrameOnly];
1162
-  
1166
+
1163
   if(_webView != nil){
1167
   if(_webView != nil){
1164
     [self resetupScripts:_webView.configuration];
1168
     [self resetupScripts:_webView.configuration];
1165
   }
1169
   }
1167
 
1171
 
1168
 - (void)setInjectedJavaScriptBeforeContentLoaded:(NSString *)source {
1172
 - (void)setInjectedJavaScriptBeforeContentLoaded:(NSString *)source {
1169
   _injectedJavaScriptBeforeContentLoaded = source;
1173
   _injectedJavaScriptBeforeContentLoaded = source;
1170
-  
1174
+
1171
   self.atStartScript = source == nil ? nil : [[WKUserScript alloc] initWithSource:source
1175
   self.atStartScript = source == nil ? nil : [[WKUserScript alloc] initWithSource:source
1172
        injectionTime:WKUserScriptInjectionTimeAtDocumentStart
1176
        injectionTime:WKUserScriptInjectionTimeAtDocumentStart
1173
     forMainFrameOnly:_injectedJavaScriptBeforeContentLoadedForMainFrameOnly];
1177
     forMainFrameOnly:_injectedJavaScriptBeforeContentLoadedForMainFrameOnly];
1174
-  
1178
+
1175
   if(_webView != nil){
1179
   if(_webView != nil){
1176
     [self resetupScripts:_webView.configuration];
1180
     [self resetupScripts:_webView.configuration];
1177
   }
1181
   }
1189
 
1193
 
1190
 - (void)setMessagingEnabled:(BOOL)messagingEnabled {
1194
 - (void)setMessagingEnabled:(BOOL)messagingEnabled {
1191
   _messagingEnabled = messagingEnabled;
1195
   _messagingEnabled = messagingEnabled;
1192
-  
1196
+
1193
   self.postMessageScript = _messagingEnabled ?
1197
   self.postMessageScript = _messagingEnabled ?
1194
   [
1198
   [
1195
    [WKUserScript alloc]
1199
    [WKUserScript alloc]
1209
    forMainFrameOnly:YES
1213
    forMainFrameOnly:YES
1210
    ] :
1214
    ] :
1211
   nil;
1215
   nil;
1212
-  
1216
+
1213
   if(_webView != nil){
1217
   if(_webView != nil){
1214
     [self resetupScripts:_webView.configuration];
1218
     [self resetupScripts:_webView.configuration];
1215
   }
1219
   }
1218
 - (void)resetupScripts:(WKWebViewConfiguration *)wkWebViewConfig {
1222
 - (void)resetupScripts:(WKWebViewConfiguration *)wkWebViewConfig {
1219
   [wkWebViewConfig.userContentController removeAllUserScripts];
1223
   [wkWebViewConfig.userContentController removeAllUserScripts];
1220
   [wkWebViewConfig.userContentController removeScriptMessageHandlerForName:MessageHandlerName];
1224
   [wkWebViewConfig.userContentController removeScriptMessageHandlerForName:MessageHandlerName];
1221
-  
1225
+
1222
   NSString *html5HistoryAPIShimSource = [NSString stringWithFormat:
1226
   NSString *html5HistoryAPIShimSource = [NSString stringWithFormat:
1223
     @"(function(history) {\n"
1227
     @"(function(history) {\n"
1224
     "  function notify(type) {\n"
1228
     "  function notify(type) {\n"
1241
   ];
1245
   ];
1242
   WKUserScript *script = [[WKUserScript alloc] initWithSource:html5HistoryAPIShimSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
1246
   WKUserScript *script = [[WKUserScript alloc] initWithSource:html5HistoryAPIShimSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
1243
   [wkWebViewConfig.userContentController addUserScript:script];
1247
   [wkWebViewConfig.userContentController addUserScript:script];
1244
-  
1248
+
1245
   if(_sharedCookiesEnabled) {
1249
   if(_sharedCookiesEnabled) {
1246
     // More info to sending cookies with WKWebView
1250
     // More info to sending cookies with WKWebView
1247
     // https://stackoverflow.com/questions/26573137/can-i-set-the-cookies-to-be-used-by-a-wkwebview/26577303#26577303
1251
     // https://stackoverflow.com/questions/26573137/can-i-set-the-cookies-to-be-used-by-a-wkwebview/26577303#26577303
1303
       [wkWebViewConfig.userContentController addUserScript:cookieInScript];
1307
       [wkWebViewConfig.userContentController addUserScript:cookieInScript];
1304
     }
1308
     }
1305
   }
1309
   }
1306
-  
1310
+
1307
   if(_messagingEnabled){
1311
   if(_messagingEnabled){
1308
     if (self.postMessageScript){
1312
     if (self.postMessageScript){
1309
       [wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
1313
       [wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]