Skip to content

Commit 7217630

Browse files
RSNarafacebook-github-bot
authored andcommitted
Implement 'mediaPlaybackRequiresUserAction' prop
Summary: HTML video elements can have the `autoplay` attribute, which forces them to play automatically whenever they load on the page. In this diff, I introduce a new prop `mediaPlaybackRequiresUserAction`, which allows us to control whether video or audio element autoplays even when `autoplay` is set. Reviewed By: shergin Differential Revision: D6382256 fbshipit-source-id: 617508653910d600bc43f7f68c6dfd17ab1b6dd8
1 parent 4ca949b commit 7217630

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

Libraries/Components/WKWebView/WKWebView.ios.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ const RCTWKWebView = requireNativeComponent('RCTWKWebView');
1717

1818
type RCTWKWebViewProps = {
1919
allowsInlineMediaPlayback?: boolean,
20+
mediaPlaybackRequiresUserAction?: boolean,
2021
};
2122

2223
class WKWebView extends React.Component<RCTWKWebViewProps> {
2324
componentWillReceiveProps(nextProps: RCTWKWebViewProps) {
24-
if (this.props.allowsInlineMediaPlayback !== nextProps.allowsInlineMediaPlayback) {
25-
console.error('Changes to property allowsInlineMediaPlayback do nothing after the initial render.');
25+
this.showRedboxOnPropChanges(nextProps, 'allowsInlineMediaPlayback');
26+
this.showRedboxOnPropChanges(nextProps, 'mediaPlaybackRequiresUserAction');
27+
}
28+
29+
showRedboxOnPropChanges(nextProps: RCTWKWebViewProps, propName: string) {
30+
if (this.props[propName] !== nextProps[propName]) {
31+
console.error(`Changes to property ${propName} do nothing after the initial render.`);
2632
}
2733
}
2834

React/Views/RCTWKWebView.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
2929
@property (nonatomic, assign) CGFloat decelerationRate;
3030
@property (nonatomic, assign) BOOL allowsInlineMediaPlayback;
3131
@property (nonatomic, assign) BOOL bounces;
32+
@property (nonatomic, assign) BOOL mediaPlaybackRequiresUserAction;
3233

3334
- (void)postMessage:(NSString *)message;
3435
- (void)injectJavaScript:(NSString *)script;

React/Views/RCTWKWebView.m

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ - (void)didMoveToWindow
4040
wkWebViewConfig.userContentController = [WKUserContentController new];
4141
[wkWebViewConfig.userContentController addScriptMessageHandler: self name: MessageHanderName];
4242
wkWebViewConfig.allowsInlineMediaPlayback = _allowsInlineMediaPlayback;
43+
wkWebViewConfig.mediaTypesRequiringUserActionForPlayback = _mediaPlaybackRequiresUserAction
44+
? WKAudiovisualMediaTypeAll
45+
: WKAudiovisualMediaTypeNone;
4346

4447
_webView = [[WKWebView alloc] initWithFrame:self.bounds configuration: wkWebViewConfig];
4548
_webView.scrollView.delegate = self;

React/Views/RCTWKWebViewManager.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ - (UIView *)view
2828
RCT_EXPORT_VIEW_PROPERTY(onShouldStartLoadWithRequest, RCTDirectEventBlock)
2929
RCT_EXPORT_VIEW_PROPERTY(injectedJavaScript, NSString)
3030
RCT_EXPORT_VIEW_PROPERTY(allowsInlineMediaPlayback, BOOL)
31-
31+
RCT_EXPORT_VIEW_PROPERTY(mediaPlaybackRequiresUserAction, BOOL)
3232
/**
3333
* Expose methods to enable messaging the webview.
3434
*/

0 commit comments

Comments
 (0)