Ver código fonte

feat(iOS): Add callback for webViewWebContentProcessDidTerminate (#774)

* Add callback for WKWebView's onContentProcessDidTerminate

* Add platform information to docs

* Add new WebViewTerminatedEvent type
Samuel Sieg 5 anos atrás
pai
commit
a76811da93
5 arquivos alterados com 68 adições e 0 exclusões
  1. 35
    0
      docs/Reference.md
  2. 14
    0
      ios/RNCWebView.m
  3. 1
    0
      ios/RNCWebViewManager.m
  4. 9
    0
      src/WebView.ios.tsx
  5. 9
    0
      src/WebViewTypes.ts

+ 35
- 0
docs/Reference.md Ver arquivo

@@ -17,6 +17,7 @@ This document lays out the current public properties and methods for the React N
17 17
 - [`onHttpError`](Reference.md#onhttperror)
18 18
 - [`onMessage`](Reference.md#onmessage)
19 19
 - [`onNavigationStateChange`](Reference.md#onnavigationstatechange)
20
+- [`onContentProcessDidTerminate`](Reference.md#oncontentprocessdidterminate)
20 21
 - [`originWhitelist`](Reference.md#originwhitelist)
21 22
 - [`renderError`](Reference.md#rendererror)
22 23
 - [`renderLoading`](Reference.md#renderloading)
@@ -433,6 +434,40 @@ Note that this method will not be invoked on hash URL changes (e.g. from `https:
433 434
 
434 435
 ---
435 436
 
437
+### `onContentProcessDidTerminate`
438
+
439
+Function that is invoked when the `WebView` content process is terminated.
440
+
441
+| Type     | Required | Platform      |
442
+| -------- | -------- | ------------- |
443
+| function | No       | iOS WKWebView |
444
+
445
+Example:
446
+
447
+```jsx
448
+<WebView
449
+  source={{ uri: 'https://facebook.github.io/react-native' }}
450
+  onContentProcessDidTerminate={syntheticEvent => {
451
+    const { nativeEvent } = syntheticEvent;
452
+    console.warn('Content process terminated, reloading', nativeEvent);
453
+    this.refs.webview.reload()
454
+  }}
455
+/>
456
+```
457
+
458
+Function passed to onContentProcessDidTerminate is called with a SyntheticEvent wrapping a nativeEvent with these properties:
459
+
460
+```
461
+canGoBack
462
+canGoForward
463
+loading
464
+target
465
+title
466
+url
467
+```
468
+
469
+---
470
+
436 471
 ### `originWhitelist`
437 472
 
438 473
 List of origin strings to allow being navigated to. The strings allow wildcards and get matched against _just_ the origin (not the full URL). If the user taps to navigate to a new page but the new page is not in this whitelist, the URL will be handled by the OS. The default whitelisted origins are "http://*" and "https://*".

+ 14
- 0
ios/RNCWebView.m Ver arquivo

@@ -37,6 +37,7 @@ static NSDictionary* customCertificatesForHost;
37 37
 @property (nonatomic, copy) RCTDirectEventBlock onHttpError;
38 38
 @property (nonatomic, copy) RCTDirectEventBlock onMessage;
39 39
 @property (nonatomic, copy) RCTDirectEventBlock onScroll;
40
+@property (nonatomic, copy) RCTDirectEventBlock onContentProcessDidTerminate;
40 41
 @property (nonatomic, copy) WKWebView *webView;
41 42
 @end
42 43
 
@@ -833,6 +834,19 @@ static NSDictionary* customCertificatesForHost;
833 834
   decisionHandler(WKNavigationResponsePolicyAllow);
834 835
 }
835 836
 
837
+/**
838
+ * Called when the web view’s content process is terminated.
839
+ * @see https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455639-webviewwebcontentprocessdidtermi?language=objc
840
+ */
841
+- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView
842
+{
843
+  RCTLogWarn(@"Webview Process Terminated");
844
+  if (_onContentProcessDidTerminate) {
845
+    NSMutableDictionary<NSString *, id> *event = [self baseEvent];
846
+    _onContentProcessDidTerminate(event);
847
+  }
848
+}
849
+
836 850
 /**
837 851
  * Decides whether to allow or cancel a navigation after its response is known.
838 852
  * @see https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455643-webview?language=objc

+ 1
- 0
ios/RNCWebViewManager.m Ver arquivo

@@ -49,6 +49,7 @@ RCT_EXPORT_VIEW_PROPERTY(onLoadingError, RCTDirectEventBlock)
49 49
 RCT_EXPORT_VIEW_PROPERTY(onLoadingProgress, RCTDirectEventBlock)
50 50
 RCT_EXPORT_VIEW_PROPERTY(onHttpError, RCTDirectEventBlock)
51 51
 RCT_EXPORT_VIEW_PROPERTY(onShouldStartLoadWithRequest, RCTDirectEventBlock)
52
+RCT_EXPORT_VIEW_PROPERTY(onContentProcessDidTerminate, RCTDirectEventBlock)
52 53
 RCT_EXPORT_VIEW_PROPERTY(injectedJavaScript, NSString)
53 54
 RCT_EXPORT_VIEW_PROPERTY(javaScriptEnabled, BOOL)
54 55
 RCT_EXPORT_VIEW_PROPERTY(allowsInlineMediaPlayback, BOOL)

+ 9
- 0
src/WebView.ios.tsx Ver arquivo

@@ -22,6 +22,7 @@ import {
22 22
   WebViewMessageEvent,
23 23
   WebViewNavigationEvent,
24 24
   WebViewProgressEvent,
25
+  WebViewTerminatedEvent,
25 26
   IOSWebViewProps,
26 27
   DecelerationRateConstant,
27 28
   NativeWebViewIOS,
@@ -255,6 +256,13 @@ class WebView extends React.Component<IOSWebViewProps, State> {
255 256
     viewManager.startLoadWithResult(!!shouldStart, lockIdentifier);
256 257
   };
257 258
 
259
+  onContentProcessDidTerminate = (event: WebViewTerminatedEvent) => {
260
+    const { onContentProcessDidTerminate } = this.props;
261
+    if (onContentProcessDidTerminate) {
262
+      onContentProcessDidTerminate(event);
263
+    }
264
+  };
265
+
258 266
   componentDidUpdate(prevProps: IOSWebViewProps) {
259 267
     this.showRedboxOnPropChanges(prevProps, 'allowsInlineMediaPlayback');
260 268
     this.showRedboxOnPropChanges(prevProps, 'incognito');
@@ -333,6 +341,7 @@ class WebView extends React.Component<IOSWebViewProps, State> {
333 341
         onMessage={this.onMessage}
334 342
         onScroll={this.props.onScroll}
335 343
         onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
344
+        onContentProcessDidTerminate={this.onContentProcessDidTerminate}
336 345
         ref={this.webViewRef}
337 346
         // TODO: find a better way to type this.
338 347
         source={resolveAssetSource(this.props.source as ImageSourcePropType)}

+ 9
- 0
src/WebViewTypes.ts Ver arquivo

@@ -126,6 +126,8 @@ export type WebViewMessageEvent = NativeSyntheticEvent<WebViewMessage>;
126 126
 
127 127
 export type WebViewErrorEvent = NativeSyntheticEvent<WebViewError>;
128 128
 
129
+export type WebViewTerminatedEvent = NativeSyntheticEvent<WebViewNativeEvent>;
130
+
129 131
 export type WebViewHttpErrorEvent = NativeSyntheticEvent<WebViewHttpError>;
130 132
 
131 133
 export type DataDetectorTypes =
@@ -269,6 +271,7 @@ export interface IOSNativeWebViewProps extends CommonNativeWebViewProps {
269 271
   pagingEnabled?: boolean;
270 272
   scrollEnabled?: boolean;
271 273
   useSharedProcessPool?: boolean;
274
+  onContentProcessDidTerminate: (event: WebViewTerminatedEvent) => void;
272 275
 }
273 276
 
274 277
 export interface IOSWebViewProps extends WebViewSharedProps {
@@ -442,6 +445,12 @@ export interface IOSWebViewProps extends WebViewSharedProps {
442 445
    * @platform ios
443 446
    */
444 447
   allowingReadAccessToURL?: string;
448
+
449
+  /**
450
+   * Function that is invoked when the WebKit WebView content process gets terminated.
451
+   * @platform ios
452
+   */
453
+  onContentProcessDidTerminate: (event: WebViewTerminatedEvent) => void;
445 454
 }
446 455
 
447 456
 export interface AndroidWebViewProps extends WebViewSharedProps {