Skip to content

Commit 95801f1

Browse files
RSNarafacebook-github-bot
authored andcommittedAug 16, 2018
Move WKWebView into WebView.ios.js
Summary: @public This diff adds the `useWebKit` property to the `<WebView/>` React Native component. On iOS, when this property is true, we use `RCTWKWebView`. Otherwise, we use `RCTWebView`. On Android, this property does nothing. Reviewed By: shergin Differential Revision: D6423374 fbshipit-source-id: 006bfaaf12984fac0174c0b5bb897c009c026cd0
1 parent bacfd92 commit 95801f1

File tree

3 files changed

+77
-31
lines changed

3 files changed

+77
-31
lines changed
 

‎Libraries/Components/WKWebView/WKWebView.android.js

-22
This file was deleted.

‎Libraries/Components/WebView/WebView.android.js

+6
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ class WebView extends React.Component {
109109
PropTypes.number,
110110
]),
111111

112+
/**
113+
* If true, use WKWebView instead of UIWebView.
114+
* @platform ios
115+
*/
116+
useWebKit: PropTypes.bool,
117+
112118
/**
113119
* Used on Android only, JS is enabled by default for WebView on iOS
114120
* @platform android

‎Libraries/Components/WebView/WebView.ios.js

+71-9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const requireNativeComponent = require('requireNativeComponent');
3232
const resolveAssetSource = require('resolveAssetSource');
3333

3434
const RCTWebViewManager = require('NativeModules').WebViewManager;
35+
const RCTWKWebViewManager = require('NativeModules').WKWebViewManager;
3536

3637
const BGWASH = 'rgba(255,255,255,0.8)';
3738
const RCT_WEBVIEW_REF = 'webview';
@@ -66,6 +67,9 @@ const DataDetectorTypes = [
6667
'link',
6768
'address',
6869
'calendarEvent',
70+
'trackingNumber',
71+
'flightNumber',
72+
'lookupSuggestion',
6973
'none',
7074
'all',
7175
];
@@ -162,6 +166,12 @@ class WebView extends React.Component {
162166
PropTypes.number,
163167
]),
164168

169+
/**
170+
* If true, use WKWebView instead of UIWebView.
171+
* @platform ios
172+
*/
173+
useWebKit: PropTypes.bool,
174+
165175
/**
166176
* Function that returns a view to show if there's an error.
167177
*/
@@ -264,6 +274,11 @@ class WebView extends React.Component {
264274
* - `'none'`
265275
* - `'all'`
266276
*
277+
* With the new WebKit implementation, we have three new values:
278+
* - `'trackingNumber'`,
279+
* - `'flightNumber'`,
280+
* - `'lookupSuggestion'`,
281+
*
267282
* @platform ios
268283
*/
269284
dataDetectorTypes: PropTypes.oneOfType([
@@ -433,7 +448,13 @@ class WebView extends React.Component {
433448

434449
const nativeConfig = this.props.nativeConfig || {};
435450

436-
const viewManager = nativeConfig.viewManager || RCTWebViewManager;
451+
let viewManager = nativeConfig.viewManager;
452+
453+
if (this.props.useWebKit) {
454+
viewManager = viewManager || RCTWKWebViewManager;
455+
} else {
456+
viewManager = viewManager || RCTWebViewManager;
457+
}
437458

438459
const compiledWhitelist = [
439460
'about:blank',
@@ -474,7 +495,13 @@ class WebView extends React.Component {
474495

475496
const messagingEnabled = typeof this.props.onMessage === 'function';
476497

477-
const NativeWebView = nativeConfig.component || RCTWebView;
498+
let NativeWebView = nativeConfig.component;
499+
500+
if (this.props.useWebKit) {
501+
NativeWebView = NativeWebView || RCTWKWebView;
502+
} else {
503+
NativeWebView = NativeWebView || RCTWebView;
504+
}
478505

479506
const webView = (
480507
<NativeWebView
@@ -514,13 +541,21 @@ class WebView extends React.Component {
514541
);
515542
}
516543

544+
_getCommands() {
545+
if (!this.props.useWebKit) {
546+
return UIManager.RCTWebView.Commands;
547+
}
548+
549+
return UIManager.RCTWKWebView.Commands;
550+
}
551+
517552
/**
518553
* Go forward one page in the web view's history.
519554
*/
520555
goForward = () => {
521556
UIManager.dispatchViewManagerCommand(
522557
this.getWebViewHandle(),
523-
UIManager.RCTWebView.Commands.goForward,
558+
this._getCommands().goForward,
524559
null,
525560
);
526561
};
@@ -531,7 +566,7 @@ class WebView extends React.Component {
531566
goBack = () => {
532567
UIManager.dispatchViewManagerCommand(
533568
this.getWebViewHandle(),
534-
UIManager.RCTWebView.Commands.goBack,
569+
this._getCommands().goBack,
535570
null,
536571
);
537572
};
@@ -543,7 +578,7 @@ class WebView extends React.Component {
543578
this.setState({viewState: WebViewState.LOADING});
544579
UIManager.dispatchViewManagerCommand(
545580
this.getWebViewHandle(),
546-
UIManager.RCTWebView.Commands.reload,
581+
this._getCommands().reload,
547582
null,
548583
);
549584
};
@@ -554,7 +589,7 @@ class WebView extends React.Component {
554589
stopLoading = () => {
555590
UIManager.dispatchViewManagerCommand(
556591
this.getWebViewHandle(),
557-
UIManager.RCTWebView.Commands.stopLoading,
592+
this._getCommands().stopLoading,
558593
null,
559594
);
560595
};
@@ -572,7 +607,7 @@ class WebView extends React.Component {
572607
postMessage = data => {
573608
UIManager.dispatchViewManagerCommand(
574609
this.getWebViewHandle(),
575-
UIManager.RCTWebView.Commands.postMessage,
610+
this._getCommands().postMessage,
576611
[String(data)],
577612
);
578613
};
@@ -586,7 +621,7 @@ class WebView extends React.Component {
586621
injectJavaScript = data => {
587622
UIManager.dispatchViewManagerCommand(
588623
this.getWebViewHandle(),
589-
UIManager.RCTWebView.Commands.injectJavaScript,
624+
this._getCommands().injectJavaScript,
590625
[data],
591626
);
592627
};
@@ -641,9 +676,36 @@ class WebView extends React.Component {
641676
const {onMessage} = this.props;
642677
onMessage && onMessage(event);
643678
};
679+
680+
componentDidUpdate(prevProps) {
681+
if (!(prevProps.useWebKit && this.props.useWebKit)) {
682+
return;
683+
}
684+
685+
this._showRedboxOnPropChanges(prevProps, 'allowsInlineMediaPlayback');
686+
this._showRedboxOnPropChanges(prevProps, 'mediaPlaybackRequiresUserAction');
687+
this._showRedboxOnPropChanges(prevProps, 'dataDetectorTypes');
688+
}
689+
690+
_showRedboxOnPropChanges(prevProps, propName: string) {
691+
if (this.props[propName] !== prevProps[propName]) {
692+
console.error(
693+
`Changes to property ${propName} do nothing after the initial render.`,
694+
);
695+
}
696+
}
644697
}
645698

646-
const RCTWebView = requireNativeComponent('RCTWebView');
699+
const RCTWebView = requireNativeComponent(
700+
'RCTWebView',
701+
WebView,
702+
WebView.extraNativeComponentConfig,
703+
);
704+
const RCTWKWebView = requireNativeComponent(
705+
'RCTWKWebView',
706+
WebView,
707+
WebView.extraNativeComponentConfig,
708+
);
647709

648710
const styles = StyleSheet.create({
649711
container: {

0 commit comments

Comments
 (0)
Please sign in to comment.