Skip to content

Commit 4ca949b

Browse files
RSNarafacebook-github-bot
authored andcommitted
Implement 'allowsInlineMediaPlayback` prop
Summary: For iPhones with small screen sizes (e.g: iPhone 5s), inside the `<WKWebView/>` component, videos will play in fullscreen mode. In this diff, I introduce a prop called `allowsInlineMediaPlayback` that when set to true, will allow videos to play inline. **Note:** For videos to play inline, the HTML video element must also have a `playsinline` attribute on it. Reviewed By: shergin Differential Revision: D6379770 fbshipit-source-id: a0130720ffede6c24a90cad0c97a75b657d77017
1 parent a997c0a commit 4ca949b

File tree

4 files changed

+78
-25
lines changed

4 files changed

+78
-25
lines changed

Diff for: Libraries/Components/WKWebView/WKWebView.ios.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@
99
* @providesModule WKWebView
1010
*/
1111

12+
const React = require('react');
13+
1214
const requireNativeComponent = require('requireNativeComponent');
1315

14-
module.exports = requireNativeComponent('RCTWKWebView');
16+
const RCTWKWebView = requireNativeComponent('RCTWKWebView');
17+
18+
type RCTWKWebViewProps = {
19+
allowsInlineMediaPlayback?: boolean,
20+
};
21+
22+
class WKWebView extends React.Component<RCTWKWebViewProps> {
23+
componentWillReceiveProps(nextProps: RCTWKWebViewProps) {
24+
if (this.props.allowsInlineMediaPlayback !== nextProps.allowsInlineMediaPlayback) {
25+
console.error('Changes to property allowsInlineMediaPlayback do nothing after the initial render.');
26+
}
27+
}
28+
29+
render() {
30+
return <RCTWKWebView {...this.props}/>;
31+
}
32+
}
33+
34+
module.exports = WKWebView;

Diff for: React/Views/RCTWKWebView.h

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
2727
@property (nonatomic, copy) NSString *injectedJavaScript;
2828
@property (nonatomic, assign) BOOL scrollEnabled;
2929
@property (nonatomic, assign) CGFloat decelerationRate;
30+
@property (nonatomic, assign) BOOL allowsInlineMediaPlayback;
31+
@property (nonatomic, assign) BOOL bounces;
3032

3133
- (void)postMessage:(NSString *)message;
3234
- (void)injectJavaScript:(NSString *)script;

Diff for: React/Views/RCTWKWebView.m

+50-23
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,30 @@ - (instancetype)initWithFrame:(CGRect)frame
2727
{
2828
if ((self = [super initWithFrame:frame])) {
2929
super.backgroundColor = [UIColor clearColor];
30+
_bounces = YES;
31+
_scrollEnabled = YES;
32+
}
33+
return self;
34+
}
35+
36+
- (void)didMoveToWindow
37+
{
38+
if (self.window != nil) {
3039
WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new];
3140
wkWebViewConfig.userContentController = [WKUserContentController new];
3241
[wkWebViewConfig.userContentController addScriptMessageHandler: self name: MessageHanderName];
42+
wkWebViewConfig.allowsInlineMediaPlayback = _allowsInlineMediaPlayback;
3343

3444
_webView = [[WKWebView alloc] initWithFrame:self.bounds configuration: wkWebViewConfig];
3545
_webView.scrollView.delegate = self;
3646
_webView.UIDelegate = self;
3747
_webView.navigationDelegate = self;
48+
_webView.scrollView.scrollEnabled = _scrollEnabled;
49+
_webView.scrollView.bounces = _bounces;
3850
[self addSubview:_webView];
51+
52+
[self visitSource];
3953
}
40-
return self;
4154
}
4255

4356
/**
@@ -59,32 +72,39 @@ - (void)setSource:(NSDictionary *)source
5972
if (![_source isEqualToDictionary:source]) {
6073
_source = [source copy];
6174

62-
// Check for a static html source first
63-
NSString *html = [RCTConvert NSString:source[@"html"]];
64-
if (html) {
65-
NSURL *baseURL = [RCTConvert NSURL:source[@"baseUrl"]];
66-
if (!baseURL) {
67-
baseURL = [NSURL URLWithString:@"about:blank"];
68-
}
69-
[_webView loadHTMLString:html baseURL:baseURL];
70-
return;
75+
if (_webView != nil) {
76+
[self visitSource];
7177
}
78+
}
79+
}
7280

73-
NSURLRequest *request = [RCTConvert NSURLRequest:source];
74-
// Because of the way React works, as pages redirect, we actually end up
75-
// passing the redirect urls back here, so we ignore them if trying to load
76-
// the same url. We'll expose a call to 'reload' to allow a user to load
77-
// the existing page.
78-
if ([request.URL isEqual:_webView.URL]) {
79-
return;
80-
}
81-
if (!request.URL) {
82-
// Clear the webview
83-
[_webView loadHTMLString:@"" baseURL:nil];
84-
return;
81+
- (void)visitSource
82+
{
83+
// Check for a static html source first
84+
NSString *html = [RCTConvert NSString:_source[@"html"]];
85+
if (html) {
86+
NSURL *baseURL = [RCTConvert NSURL:_source[@"baseUrl"]];
87+
if (!baseURL) {
88+
baseURL = [NSURL URLWithString:@"about:blank"];
8589
}
86-
[_webView loadRequest:request];
90+
[_webView loadHTMLString:html baseURL:baseURL];
91+
return;
8792
}
93+
94+
NSURLRequest *request = [RCTConvert NSURLRequest:_source];
95+
// Because of the way React works, as pages redirect, we actually end up
96+
// passing the redirect urls back here, so we ignore them if trying to load
97+
// the same url. We'll expose a call to 'reload' to allow a user to load
98+
// the existing page.
99+
if ([request.URL isEqual:_webView.URL]) {
100+
return;
101+
}
102+
if (!request.URL) {
103+
// Clear the webview
104+
[_webView loadHTMLString:@"" baseURL:nil];
105+
return;
106+
}
107+
[_webView loadRequest:request];
88108
}
89109

90110

@@ -95,6 +115,7 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
95115

96116
- (void)setScrollEnabled:(BOOL)scrollEnabled
97117
{
118+
_scrollEnabled = scrollEnabled;
98119
_webView.scrollView.scrollEnabled = scrollEnabled;
99120
}
100121

@@ -305,4 +326,10 @@ - (void)stopLoading
305326
{
306327
[_webView stopLoading];
307328
}
329+
330+
- (void)setBounces:(BOOL)bounces
331+
{
332+
_bounces = bounces;
333+
_webView.scrollView.bounces = bounces;
334+
}
308335
@end

Diff for: React/Views/RCTWKWebViewManager.m

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ - (UIView *)view
2727
RCT_EXPORT_VIEW_PROPERTY(onLoadingError, RCTDirectEventBlock)
2828
RCT_EXPORT_VIEW_PROPERTY(onShouldStartLoadWithRequest, RCTDirectEventBlock)
2929
RCT_EXPORT_VIEW_PROPERTY(injectedJavaScript, NSString)
30+
RCT_EXPORT_VIEW_PROPERTY(allowsInlineMediaPlayback, BOOL)
3031

3132
/**
3233
* Expose methods to enable messaging the webview.
@@ -46,7 +47,10 @@ - (UIView *)view
4647
}];
4748
}
4849

49-
RCT_REMAP_VIEW_PROPERTY(bounces, _webView.scrollView.bounces, BOOL)
50+
RCT_CUSTOM_VIEW_PROPERTY(bounces, BOOL, RCTWKWebView) {
51+
view.bounces = json == nil ? true : [RCTConvert BOOL: json];
52+
}
53+
5054
RCT_CUSTOM_VIEW_PROPERTY(scrollEnabled, BOOL, RCTWKWebView) {
5155
view.scrollEnabled = json == nil ? true : [RCTConvert BOOL: json];
5256
}

0 commit comments

Comments
 (0)