2018-08-17 01:34:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
2018-08-16 20:33:53 +00:00
|
|
|
#import "RCTWKWebView.h"
|
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
#import "RCTAutoInsetsProtocol.h"
|
|
|
|
|
2018-08-16 20:33:58 +00:00
|
|
|
static NSString *const MessageHanderName = @"ReactNative";
|
|
|
|
|
2018-08-16 20:34:26 +00:00
|
|
|
@interface RCTWKWebView () <WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler, UIScrollViewDelegate, RCTAutoInsetsProtocol>
|
2018-08-16 20:33:55 +00:00
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onLoadingStart;
|
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onLoadingFinish;
|
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onLoadingError;
|
2018-08-16 20:34:16 +00:00
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onShouldStartLoadWithRequest;
|
2018-08-16 20:33:58 +00:00
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onMessage;
|
2018-08-16 20:33:55 +00:00
|
|
|
@property (nonatomic, copy) WKWebView *webView;
|
2018-08-16 20:33:53 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTWKWebView
|
|
|
|
{
|
Implement 'backgroundColor' style
Summary:
@public
This diff implements background colors for the `RCTWKWebView` component by proxying the background color prop to the underlying `WKWebView` and its underlying `UIScrollView`.
There's few differences between `backgroundColor` in `RCTWebView` and `RCTWKWebView` implementations:
1. With `UIWebView,` the background color gets applied after the page loads. With `WKWebView`, this isn't necessarily true. This results in a white flicker on solid backgrounds because sometimes, the background color is set before the page loads. This video illustrates the problem: https://our.intern.facebook.com/intern/px/p/9QBH
1. As far as I can tell, `WKWebView` doesn't handle transparent backgrounds correctly. Either that, or I could be setting the background color incorrectly. I set the background color to `rgba(1, 1, 1, 0.5)` and recorded how both `RCTWebView` and `RCTWKWebView` render. These two videos indicate the differences:
**RCTWebView: Lighter background**
https://pxl.cl/9R13
**RCTWKWebView: Darker background**
https://pxl.cl/9R1b
I tried to replicate this on the web. According to [[ https://our.intern.facebook.com/intern/fiddle/zCHu/ | this fiddle ]], `RCTWebView` is correct. Clearly, RCTWKWebView is rendering transparent backgrounds a bit darker than necessary. This doesn't seem simple to debug, so I've created a task to document this work: T23815343. I'll get to it eventually.
Reviewed By: shergin
Differential Revision: D6398209
fbshipit-source-id: 1812cb68133bc18a3278f6b328d7b085362528b0
2018-08-16 20:34:24 +00:00
|
|
|
UIColor * _savedBackgroundColor;
|
2018-08-16 20:33:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:34:33 +00:00
|
|
|
/**
|
|
|
|
* See https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/WebKitAvail.html.
|
|
|
|
*/
|
|
|
|
+ (BOOL)dynamicallyLoadWebKitIfAvailable
|
|
|
|
{
|
|
|
|
static BOOL _webkitAvailable=NO;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
NSBundle *webKitBundle = [NSBundle bundleWithPath:@"/System/Library/Frameworks/WebKit.framework"];
|
|
|
|
if (webKitBundle) {
|
|
|
|
_webkitAvailable = [webKitBundle load];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return _webkitAvailable;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-16 20:33:53 +00:00
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
if ((self = [super initWithFrame:frame])) {
|
|
|
|
super.backgroundColor = [UIColor clearColor];
|
2018-08-16 20:34:18 +00:00
|
|
|
_bounces = YES;
|
|
|
|
_scrollEnabled = YES;
|
2018-08-16 20:34:26 +00:00
|
|
|
_automaticallyAdjustContentInsets = YES;
|
|
|
|
_contentInset = UIEdgeInsetsZero;
|
2018-08-16 20:34:18 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didMoveToWindow
|
|
|
|
{
|
|
|
|
if (self.window != nil) {
|
2018-08-16 20:34:33 +00:00
|
|
|
if (![[self class] dynamicallyLoadWebKitIfAvailable]) {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2018-08-16 20:33:58 +00:00
|
|
|
WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new];
|
|
|
|
wkWebViewConfig.userContentController = [WKUserContentController new];
|
|
|
|
[wkWebViewConfig.userContentController addScriptMessageHandler: self name: MessageHanderName];
|
2018-08-16 20:34:18 +00:00
|
|
|
wkWebViewConfig.allowsInlineMediaPlayback = _allowsInlineMediaPlayback;
|
2018-08-16 20:34:35 +00:00
|
|
|
#if WEBKIT_IOS_10_APIS_AVAILABLE
|
2018-08-16 20:34:20 +00:00
|
|
|
wkWebViewConfig.mediaTypesRequiringUserActionForPlayback = _mediaPlaybackRequiresUserAction
|
|
|
|
? WKAudiovisualMediaTypeAll
|
|
|
|
: WKAudiovisualMediaTypeNone;
|
2018-08-16 20:34:35 +00:00
|
|
|
wkWebViewConfig.dataDetectorTypes = _dataDetectorTypes;
|
|
|
|
#endif
|
2018-08-16 20:33:58 +00:00
|
|
|
|
|
|
|
_webView = [[WKWebView alloc] initWithFrame:self.bounds configuration: wkWebViewConfig];
|
2018-08-16 20:34:07 +00:00
|
|
|
_webView.scrollView.delegate = self;
|
2018-08-16 20:33:53 +00:00
|
|
|
_webView.UIDelegate = self;
|
2018-08-16 20:33:55 +00:00
|
|
|
_webView.navigationDelegate = self;
|
2018-08-16 20:34:18 +00:00
|
|
|
_webView.scrollView.scrollEnabled = _scrollEnabled;
|
|
|
|
_webView.scrollView.bounces = _bounces;
|
2018-08-16 20:34:26 +00:00
|
|
|
|
|
|
|
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
|
|
|
|
if ([_webView.scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
|
|
|
|
_webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-08-16 20:33:53 +00:00
|
|
|
[self addSubview:_webView];
|
2018-08-16 20:34:18 +00:00
|
|
|
|
|
|
|
[self visitSource];
|
2018-08-16 20:33:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Implement 'backgroundColor' style
Summary:
@public
This diff implements background colors for the `RCTWKWebView` component by proxying the background color prop to the underlying `WKWebView` and its underlying `UIScrollView`.
There's few differences between `backgroundColor` in `RCTWebView` and `RCTWKWebView` implementations:
1. With `UIWebView,` the background color gets applied after the page loads. With `WKWebView`, this isn't necessarily true. This results in a white flicker on solid backgrounds because sometimes, the background color is set before the page loads. This video illustrates the problem: https://our.intern.facebook.com/intern/px/p/9QBH
1. As far as I can tell, `WKWebView` doesn't handle transparent backgrounds correctly. Either that, or I could be setting the background color incorrectly. I set the background color to `rgba(1, 1, 1, 0.5)` and recorded how both `RCTWebView` and `RCTWKWebView` render. These two videos indicate the differences:
**RCTWebView: Lighter background**
https://pxl.cl/9R13
**RCTWKWebView: Darker background**
https://pxl.cl/9R1b
I tried to replicate this on the web. According to [[ https://our.intern.facebook.com/intern/fiddle/zCHu/ | this fiddle ]], `RCTWebView` is correct. Clearly, RCTWKWebView is rendering transparent backgrounds a bit darker than necessary. This doesn't seem simple to debug, so I've created a task to document this work: T23815343. I'll get to it eventually.
Reviewed By: shergin
Differential Revision: D6398209
fbshipit-source-id: 1812cb68133bc18a3278f6b328d7b085362528b0
2018-08-16 20:34:24 +00:00
|
|
|
- (void)setBackgroundColor:(UIColor *)backgroundColor
|
|
|
|
{
|
|
|
|
_savedBackgroundColor = backgroundColor;
|
|
|
|
if (_webView == nil) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGFloat alpha = CGColorGetAlpha(backgroundColor.CGColor);
|
|
|
|
self.opaque = _webView.opaque = (alpha == 1.0);
|
|
|
|
_webView.scrollView.backgroundColor = backgroundColor;
|
|
|
|
_webView.backgroundColor = backgroundColor;
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:33:58 +00:00
|
|
|
/**
|
|
|
|
* This method is called whenever JavaScript running within the web view calls:
|
|
|
|
* - window.webkit.messageHandlers.[MessageHanderName].postMessage
|
|
|
|
*/
|
|
|
|
- (void)userContentController:(WKUserContentController *)userContentController
|
|
|
|
didReceiveScriptMessage:(WKScriptMessage *)message
|
|
|
|
{
|
|
|
|
if (_onMessage != nil) {
|
|
|
|
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
|
|
|
|
[event addEntriesFromDictionary: @{@"data": message.body}];
|
|
|
|
_onMessage(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:33:53 +00:00
|
|
|
- (void)setSource:(NSDictionary *)source
|
|
|
|
{
|
|
|
|
if (![_source isEqualToDictionary:source]) {
|
|
|
|
_source = [source copy];
|
|
|
|
|
2018-08-16 20:34:18 +00:00
|
|
|
if (_webView != nil) {
|
|
|
|
[self visitSource];
|
2018-08-16 20:33:53 +00:00
|
|
|
}
|
2018-08-16 20:34:18 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-16 20:33:53 +00:00
|
|
|
|
2018-08-16 20:34:26 +00:00
|
|
|
- (void)setContentInset:(UIEdgeInsets)contentInset
|
|
|
|
{
|
|
|
|
_contentInset = contentInset;
|
|
|
|
[RCTView autoAdjustInsetsForView:self
|
|
|
|
withScrollView:_webView.scrollView
|
|
|
|
updateOffset:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)refreshContentInset
|
|
|
|
{
|
|
|
|
[RCTView autoAdjustInsetsForView:self
|
|
|
|
withScrollView:_webView.scrollView
|
|
|
|
updateOffset:YES];
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:34:18 +00:00
|
|
|
- (void)visitSource
|
|
|
|
{
|
|
|
|
// Check for a static html source first
|
|
|
|
NSString *html = [RCTConvert NSString:_source[@"html"]];
|
|
|
|
if (html) {
|
|
|
|
NSURL *baseURL = [RCTConvert NSURL:_source[@"baseUrl"]];
|
|
|
|
if (!baseURL) {
|
|
|
|
baseURL = [NSURL URLWithString:@"about:blank"];
|
2018-08-16 20:33:53 +00:00
|
|
|
}
|
2018-08-16 20:34:18 +00:00
|
|
|
[_webView loadHTMLString:html baseURL:baseURL];
|
|
|
|
return;
|
2018-08-16 20:33:53 +00:00
|
|
|
}
|
2018-08-16 20:34:18 +00:00
|
|
|
|
|
|
|
NSURLRequest *request = [RCTConvert NSURLRequest:_source];
|
|
|
|
// Because of the way React works, as pages redirect, we actually end up
|
|
|
|
// passing the redirect urls back here, so we ignore them if trying to load
|
|
|
|
// the same url. We'll expose a call to 'reload' to allow a user to load
|
|
|
|
// the existing page.
|
|
|
|
if ([request.URL isEqual:_webView.URL]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!request.URL) {
|
|
|
|
// Clear the webview
|
|
|
|
[_webView loadHTMLString:@"" baseURL:nil];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[_webView loadRequest:request];
|
2018-08-16 20:33:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 20:34:07 +00:00
|
|
|
|
|
|
|
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
|
|
|
|
{
|
|
|
|
scrollView.decelerationRate = _decelerationRate;
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:34:05 +00:00
|
|
|
- (void)setScrollEnabled:(BOOL)scrollEnabled
|
|
|
|
{
|
2018-08-16 20:34:18 +00:00
|
|
|
_scrollEnabled = scrollEnabled;
|
2018-08-16 20:34:05 +00:00
|
|
|
_webView.scrollView.scrollEnabled = scrollEnabled;
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:33:58 +00:00
|
|
|
- (void)postMessage:(NSString *)message
|
|
|
|
{
|
|
|
|
NSDictionary *eventInitDict = @{@"data": message};
|
|
|
|
NSString *source = [NSString
|
|
|
|
stringWithFormat:@"document.dispatchEvent(new MessageEvent('message', %@));",
|
|
|
|
RCTJSONStringify(eventInitDict, NULL)
|
|
|
|
];
|
|
|
|
[self evaluateJS: source thenCall: nil];
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:33:53 +00:00
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
2018-08-16 20:33:58 +00:00
|
|
|
|
|
|
|
// Ensure webview takes the position and dimensions of RCTWKWebView
|
2018-08-16 20:33:53 +00:00
|
|
|
_webView.frame = self.bounds;
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:33:55 +00:00
|
|
|
- (NSMutableDictionary<NSString *, id> *)baseEvent
|
|
|
|
{
|
|
|
|
NSDictionary *event = @{
|
|
|
|
@"url": _webView.URL.absoluteString ?: @"",
|
|
|
|
@"title": _webView.title,
|
|
|
|
@"loading" : @(_webView.loading),
|
|
|
|
@"canGoBack": @(_webView.canGoBack),
|
|
|
|
@"canGoForward" : @(_webView.canGoForward)
|
|
|
|
};
|
|
|
|
return [[NSMutableDictionary alloc] initWithDictionary: event];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - WKNavigationDelegate methods
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decides whether to allow or cancel a navigation.
|
|
|
|
* @see https://fburl.com/42r9fxob
|
|
|
|
*/
|
|
|
|
- (void) webView:(WKWebView *)webView
|
|
|
|
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
|
|
|
|
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
|
|
|
|
{
|
|
|
|
static NSDictionary<NSNumber *, NSString *> *navigationTypes;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
navigationTypes = @{
|
|
|
|
@(WKNavigationTypeLinkActivated): @"click",
|
|
|
|
@(WKNavigationTypeFormSubmitted): @"formsubmit",
|
|
|
|
@(WKNavigationTypeBackForward): @"backforward",
|
|
|
|
@(WKNavigationTypeReload): @"reload",
|
|
|
|
@(WKNavigationTypeFormResubmitted): @"formresubmit",
|
|
|
|
@(WKNavigationTypeOther): @"other",
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
WKNavigationType navigationType = navigationAction.navigationType;
|
|
|
|
NSURLRequest *request = navigationAction.request;
|
|
|
|
|
2018-08-16 20:34:16 +00:00
|
|
|
if (_onShouldStartLoadWithRequest) {
|
|
|
|
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
|
|
|
|
[event addEntriesFromDictionary: @{
|
|
|
|
@"url": (request.URL).absoluteString,
|
|
|
|
@"navigationType": navigationTypes[@(navigationType)]
|
|
|
|
}];
|
|
|
|
if (![self.delegate webView:self
|
|
|
|
shouldStartLoadForRequest:event
|
|
|
|
withCallback:_onShouldStartLoadWithRequest]) {
|
|
|
|
decisionHandler(WKNavigationResponsePolicyCancel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:33:55 +00:00
|
|
|
if (_onLoadingStart) {
|
|
|
|
// We have this check to filter out iframe requests and whatnot
|
|
|
|
BOOL isTopFrame = [request.URL isEqual:request.mainDocumentURL];
|
|
|
|
if (isTopFrame) {
|
|
|
|
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
|
|
|
|
[event addEntriesFromDictionary: @{
|
|
|
|
@"url": (request.URL).absoluteString,
|
|
|
|
@"navigationType": navigationTypes[@(navigationType)]
|
|
|
|
}];
|
|
|
|
_onLoadingStart(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow all navigation by default
|
|
|
|
decisionHandler(WKNavigationResponsePolicyAllow);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when an error occurs while the web view is loading content.
|
|
|
|
* @see https://fburl.com/km6vqenw
|
|
|
|
*/
|
|
|
|
- (void) webView:(WKWebView *)webView
|
|
|
|
didFailProvisionalNavigation:(WKNavigation *)navigation
|
|
|
|
withError:(NSError *)error
|
|
|
|
{
|
|
|
|
if (_onLoadingError) {
|
|
|
|
if ([error.domain isEqualToString:NSURLErrorDomain] && error.code == NSURLErrorCancelled) {
|
|
|
|
// NSURLErrorCancelled is reported when a page has a redirect OR if you load
|
|
|
|
// a new URL in the WebView before the previous one came back. We can just
|
|
|
|
// ignore these since they aren't real errors.
|
|
|
|
// http://stackoverflow.com/questions/1024748/how-do-i-fix-nsurlerrordomain-error-999-in-iphone-3-0-os
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
|
|
|
|
[event addEntriesFromDictionary:@{
|
|
|
|
@"didFailProvisionalNavigation": @YES,
|
|
|
|
@"domain": error.domain,
|
|
|
|
@"code": @(error.code),
|
|
|
|
@"description": error.localizedDescription,
|
|
|
|
}];
|
|
|
|
_onLoadingError(event);
|
|
|
|
}
|
Implement 'backgroundColor' style
Summary:
@public
This diff implements background colors for the `RCTWKWebView` component by proxying the background color prop to the underlying `WKWebView` and its underlying `UIScrollView`.
There's few differences between `backgroundColor` in `RCTWebView` and `RCTWKWebView` implementations:
1. With `UIWebView,` the background color gets applied after the page loads. With `WKWebView`, this isn't necessarily true. This results in a white flicker on solid backgrounds because sometimes, the background color is set before the page loads. This video illustrates the problem: https://our.intern.facebook.com/intern/px/p/9QBH
1. As far as I can tell, `WKWebView` doesn't handle transparent backgrounds correctly. Either that, or I could be setting the background color incorrectly. I set the background color to `rgba(1, 1, 1, 0.5)` and recorded how both `RCTWebView` and `RCTWKWebView` render. These two videos indicate the differences:
**RCTWebView: Lighter background**
https://pxl.cl/9R13
**RCTWKWebView: Darker background**
https://pxl.cl/9R1b
I tried to replicate this on the web. According to [[ https://our.intern.facebook.com/intern/fiddle/zCHu/ | this fiddle ]], `RCTWebView` is correct. Clearly, RCTWKWebView is rendering transparent backgrounds a bit darker than necessary. This doesn't seem simple to debug, so I've created a task to document this work: T23815343. I'll get to it eventually.
Reviewed By: shergin
Differential Revision: D6398209
fbshipit-source-id: 1812cb68133bc18a3278f6b328d7b085362528b0
2018-08-16 20:34:24 +00:00
|
|
|
|
|
|
|
[self setBackgroundColor: _savedBackgroundColor];
|
2018-08-16 20:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)evaluateJS:(NSString *)js
|
|
|
|
thenCall: (void (^)(NSString*)) callback
|
|
|
|
{
|
|
|
|
[self.webView evaluateJavaScript: js completionHandler: ^(id result, NSError *error) {
|
2018-08-16 20:33:58 +00:00
|
|
|
if (error == nil && callback != nil) {
|
2018-08-16 20:33:55 +00:00
|
|
|
callback([NSString stringWithFormat:@"%@", result]);
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the navigation is complete.
|
|
|
|
* @see https://fburl.com/rtys6jlb
|
|
|
|
*/
|
|
|
|
- (void) webView:(WKWebView *)webView
|
|
|
|
didFinishNavigation:(WKNavigation *)navigation
|
|
|
|
{
|
2018-08-16 20:33:58 +00:00
|
|
|
if (_messagingEnabled) {
|
|
|
|
#if RCT_DEV
|
|
|
|
|
|
|
|
// Implementation inspired by Lodash.isNative.
|
|
|
|
NSString *isPostMessageNative = @"String(String(window.postMessage) === String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage'))";
|
|
|
|
[self evaluateJS: isPostMessageNative thenCall: ^(NSString *result) {
|
|
|
|
if (! [result isEqualToString:@"true"]) {
|
|
|
|
RCTLogError(@"Setting onMessage on a WebView overrides existing values of window.postMessage, but a previous value was defined");
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NSString *source = [NSString stringWithFormat:
|
|
|
|
@"(function() {"
|
|
|
|
"window.originalPostMessage = window.postMessage;"
|
|
|
|
|
|
|
|
"window.postMessage = function(data) {"
|
|
|
|
"window.webkit.messageHandlers.%@.postMessage(String(data));"
|
|
|
|
"};"
|
|
|
|
"})();",
|
|
|
|
MessageHanderName
|
|
|
|
];
|
|
|
|
[self evaluateJS: source thenCall: nil];
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:33:55 +00:00
|
|
|
if (_injectedJavaScript) {
|
|
|
|
[self evaluateJS: _injectedJavaScript thenCall: ^(NSString *jsEvaluationValue) {
|
|
|
|
NSMutableDictionary *event = [self baseEvent];
|
|
|
|
event[@"jsEvaluationValue"] = jsEvaluationValue;
|
|
|
|
if (self.onLoadingFinish) {
|
|
|
|
self.onLoadingFinish(event);
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
} else if (_onLoadingFinish) {
|
|
|
|
_onLoadingFinish([self baseEvent]);
|
|
|
|
}
|
Implement 'backgroundColor' style
Summary:
@public
This diff implements background colors for the `RCTWKWebView` component by proxying the background color prop to the underlying `WKWebView` and its underlying `UIScrollView`.
There's few differences between `backgroundColor` in `RCTWebView` and `RCTWKWebView` implementations:
1. With `UIWebView,` the background color gets applied after the page loads. With `WKWebView`, this isn't necessarily true. This results in a white flicker on solid backgrounds because sometimes, the background color is set before the page loads. This video illustrates the problem: https://our.intern.facebook.com/intern/px/p/9QBH
1. As far as I can tell, `WKWebView` doesn't handle transparent backgrounds correctly. Either that, or I could be setting the background color incorrectly. I set the background color to `rgba(1, 1, 1, 0.5)` and recorded how both `RCTWebView` and `RCTWKWebView` render. These two videos indicate the differences:
**RCTWebView: Lighter background**
https://pxl.cl/9R13
**RCTWKWebView: Darker background**
https://pxl.cl/9R1b
I tried to replicate this on the web. According to [[ https://our.intern.facebook.com/intern/fiddle/zCHu/ | this fiddle ]], `RCTWebView` is correct. Clearly, RCTWKWebView is rendering transparent backgrounds a bit darker than necessary. This doesn't seem simple to debug, so I've created a task to document this work: T23815343. I'll get to it eventually.
Reviewed By: shergin
Differential Revision: D6398209
fbshipit-source-id: 1812cb68133bc18a3278f6b328d7b085362528b0
2018-08-16 20:34:24 +00:00
|
|
|
|
|
|
|
[self setBackgroundColor: _savedBackgroundColor];
|
2018-08-16 20:33:55 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 20:34:09 +00:00
|
|
|
- (void)injectJavaScript:(NSString *)script
|
|
|
|
{
|
|
|
|
[self evaluateJS: script thenCall: nil];
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:34:11 +00:00
|
|
|
- (void)goForward
|
|
|
|
{
|
|
|
|
[_webView goForward];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)goBack
|
|
|
|
{
|
|
|
|
[_webView goBack];
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:34:14 +00:00
|
|
|
- (void)reload
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* When the initial load fails due to network connectivity issues,
|
|
|
|
* [_webView reload] doesn't reload the webpage. Therefore, we must
|
|
|
|
* manually call [_webView loadRequest:request].
|
|
|
|
*/
|
|
|
|
NSURLRequest *request = [RCTConvert NSURLRequest:self.source];
|
|
|
|
if (request.URL && !_webView.URL.absoluteString.length) {
|
|
|
|
[_webView loadRequest:request];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
[_webView reload];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopLoading
|
|
|
|
{
|
|
|
|
[_webView stopLoading];
|
|
|
|
}
|
2018-08-16 20:34:18 +00:00
|
|
|
|
|
|
|
- (void)setBounces:(BOOL)bounces
|
|
|
|
{
|
|
|
|
_bounces = bounces;
|
|
|
|
_webView.scrollView.bounces = bounces;
|
|
|
|
}
|
2018-08-16 20:33:53 +00:00
|
|
|
@end
|