2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-03-14 08:22:25 +00:00
|
|
|
|
|
|
|
#import "RCTWebView.h"
|
|
|
|
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
|
|
|
#import "RCTAutoInsetsProtocol.h"
|
2016-02-02 02:00:18 +00:00
|
|
|
#import "RCTConvert.h"
|
2015-03-14 08:22:25 +00:00
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
#import "RCTLog.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
#import "RCTView.h"
|
2015-03-26 09:58:06 +00:00
|
|
|
#import "UIView+React.h"
|
2015-03-14 08:22:25 +00:00
|
|
|
|
2015-07-02 01:06:39 +00:00
|
|
|
NSString *const RCTJSNavigationScheme = @"react-js-navigation";
|
|
|
|
|
2015-03-14 08:22:25 +00:00
|
|
|
@interface RCTWebView () <UIWebViewDelegate, RCTAutoInsetsProtocol>
|
|
|
|
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onLoadingStart;
|
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onLoadingFinish;
|
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onLoadingError;
|
2015-11-04 16:42:28 +00:00
|
|
|
@property (nonatomic, copy) RCTDirectEventBlock onShouldStartLoadWithRequest;
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
|
2015-03-14 08:22:25 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTWebView
|
|
|
|
{
|
|
|
|
UIWebView *_webView;
|
2015-07-08 00:07:52 +00:00
|
|
|
NSString *_injectedJavaScript;
|
2015-03-14 08:22:25 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 10:49:12 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
_webView.delegate = nil;
|
|
|
|
}
|
|
|
|
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
2015-03-14 08:22:25 +00:00
|
|
|
{
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
if ((self = [super initWithFrame:frame])) {
|
2015-04-21 01:01:46 +00:00
|
|
|
super.backgroundColor = [UIColor clearColor];
|
2015-03-14 08:22:25 +00:00
|
|
|
_automaticallyAdjustContentInsets = YES;
|
|
|
|
_contentInset = UIEdgeInsetsZero;
|
|
|
|
_webView = [[UIWebView alloc] initWithFrame:self.bounds];
|
|
|
|
_webView.delegate = self;
|
|
|
|
[self addSubview:_webView];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-03-14 08:22:25 +00:00
|
|
|
- (void)goForward
|
|
|
|
{
|
|
|
|
[_webView goForward];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)goBack
|
|
|
|
{
|
|
|
|
[_webView goBack];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reload
|
|
|
|
{
|
|
|
|
[_webView reload];
|
|
|
|
}
|
|
|
|
|
2016-02-02 02:00:18 +00:00
|
|
|
- (void)setSource:(NSDictionary *)source
|
2015-06-15 14:53:45 +00:00
|
|
|
{
|
2016-02-02 02:00:18 +00:00
|
|
|
if (![_source isEqualToDictionary:source]) {
|
|
|
|
_source = [source copy];
|
|
|
|
|
|
|
|
// Check for a static html source first
|
|
|
|
NSString *html = [RCTConvert NSString:source[@"html"]];
|
|
|
|
if (html) {
|
|
|
|
NSURL *baseURL = [RCTConvert NSURL:source[@"baseUrl"]];
|
|
|
|
[_webView loadHTMLString:html baseURL:baseURL];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.request.URL]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!request.URL) {
|
|
|
|
// Clear the webview
|
|
|
|
[_webView loadHTMLString:@"" baseURL:nil];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[_webView loadRequest:request];
|
2015-03-14 08:22:25 +00:00
|
|
|
}
|
2015-03-31 17:05:59 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 08:22:25 +00:00
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
|
|
|
_webView.frame = self.bounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setContentInset:(UIEdgeInsets)contentInset
|
|
|
|
{
|
|
|
|
_contentInset = contentInset;
|
|
|
|
[RCTView autoAdjustInsetsForView:self
|
|
|
|
withScrollView:_webView.scrollView
|
|
|
|
updateOffset:NO];
|
|
|
|
}
|
|
|
|
|
2015-04-21 01:01:46 +00:00
|
|
|
- (void)setBackgroundColor:(UIColor *)backgroundColor
|
|
|
|
{
|
|
|
|
CGFloat alpha = CGColorGetAlpha(backgroundColor.CGColor);
|
|
|
|
self.opaque = _webView.opaque = (alpha == 1.0);
|
|
|
|
_webView.backgroundColor = backgroundColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIColor *)backgroundColor
|
|
|
|
{
|
|
|
|
return _webView.backgroundColor;
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (NSMutableDictionary<NSString *, id> *)baseEvent
|
2015-03-14 08:22:25 +00:00
|
|
|
{
|
2015-11-14 18:25:00 +00:00
|
|
|
NSMutableDictionary<NSString *, id> *event = [[NSMutableDictionary alloc] initWithDictionary:@{
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
@"url": _webView.request.URL.absoluteString ?: @"",
|
2015-03-14 08:22:25 +00:00
|
|
|
@"loading" : @(_webView.loading),
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
@"title": [_webView stringByEvaluatingJavaScriptFromString:@"document.title"],
|
2015-08-24 10:14:33 +00:00
|
|
|
@"canGoBack": @(_webView.canGoBack),
|
|
|
|
@"canGoForward" : @(_webView.canGoForward),
|
2015-03-14 08:22:25 +00:00
|
|
|
}];
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2015-09-04 12:50:01 +00:00
|
|
|
- (void)refreshContentInset
|
|
|
|
{
|
|
|
|
[RCTView autoAdjustInsetsForView:self
|
|
|
|
withScrollView:_webView.scrollView
|
|
|
|
updateOffset:YES];
|
|
|
|
}
|
|
|
|
|
2015-03-14 08:22:25 +00:00
|
|
|
#pragma mark - UIWebViewDelegate methods
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (BOOL)webView:(__unused UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
|
2015-03-14 08:22:25 +00:00
|
|
|
navigationType:(UIWebViewNavigationType)navigationType
|
|
|
|
{
|
2015-11-04 16:42:28 +00:00
|
|
|
BOOL isJSNavigation = [request.URL.scheme isEqualToString:RCTJSNavigationScheme];
|
|
|
|
|
|
|
|
// skip this for the JS Navigation handler
|
|
|
|
if (!isJSNavigation && _onShouldStartLoadWithRequest) {
|
2015-11-14 18:25:00 +00:00
|
|
|
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
|
2015-11-04 16:42:28 +00:00
|
|
|
[event addEntriesFromDictionary: @{
|
|
|
|
@"url": (request.URL).absoluteString,
|
|
|
|
@"navigationType": @(navigationType)
|
|
|
|
}];
|
|
|
|
if (![self.delegate webView:self
|
|
|
|
shouldStartLoadForRequest:event
|
|
|
|
withCallback:_onShouldStartLoadWithRequest]) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
if (_onLoadingStart) {
|
|
|
|
// We have this check to filter out iframe requests and whatnot
|
|
|
|
BOOL isTopFrame = [request.URL isEqual:request.mainDocumentURL];
|
|
|
|
if (isTopFrame) {
|
2015-11-14 18:25:00 +00:00
|
|
|
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
[event addEntriesFromDictionary: @{
|
|
|
|
@"url": (request.URL).absoluteString,
|
|
|
|
@"navigationType": @(navigationType)
|
|
|
|
}];
|
|
|
|
_onLoadingStart(event);
|
|
|
|
}
|
2015-03-14 08:22:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:06:39 +00:00
|
|
|
// JS Navigation handler
|
2015-11-04 16:42:28 +00:00
|
|
|
return !isJSNavigation;
|
2015-03-14 08:22:25 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (void)webView:(__unused UIWebView *)webView didFailLoadWithError:(NSError *)error
|
2015-03-14 08:22:25 +00:00
|
|
|
{
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
|
2015-11-04 16:42:28 +00:00
|
|
|
[event addEntriesFromDictionary:@{
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
@"domain": error.domain,
|
|
|
|
@"code": @(error.code),
|
|
|
|
@"description": error.localizedDescription,
|
|
|
|
}];
|
|
|
|
_onLoadingError(event);
|
|
|
|
}
|
2015-03-14 08:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)webViewDidFinishLoad:(UIWebView *)webView
|
|
|
|
{
|
2015-07-08 00:07:52 +00:00
|
|
|
if (_injectedJavaScript != nil) {
|
Send the evaluation of the injectedJavaScript prop back to the React Native side
Summary: We have a use case in the development of our app, where we are rendering some HTML content in a web view. But this content comes from the back end, and varies in height. There are other components on the page, so it's not a full screen web view. We need to dynamically alter the web view height, to fit the HTML content without scrolling, and to push the non-web view content on the screen down.
The solution I came up with, was to use the _loadingFinish callback, to send the evaluation of the injectedJavaScript property back to the React Native side. In my example above, injecting 'document.getElement(elementId).offsetHeight' evaluates to the height of the element, with margins and borders applied, and once returned to the RN app, it can change the app state and cause the web view height to be adjusted.
Closes https://github.com/facebook/react-native/pull/2753
Reviewed By: svcscm
Differential Revision: D2578688
Pulled By: mkonicek
fb-gh-sync-id: fc9c0d0f84994a409e037016a555534549f8957a
2015-10-24 03:22:40 +00:00
|
|
|
NSString *jsEvaluationValue = [webView stringByEvaluatingJavaScriptFromString:_injectedJavaScript];
|
2015-11-04 16:42:28 +00:00
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
|
2015-11-04 16:42:28 +00:00
|
|
|
event[@"jsEvaluationValue"] = jsEvaluationValue;
|
|
|
|
|
Send the evaluation of the injectedJavaScript prop back to the React Native side
Summary: We have a use case in the development of our app, where we are rendering some HTML content in a web view. But this content comes from the back end, and varies in height. There are other components on the page, so it's not a full screen web view. We need to dynamically alter the web view height, to fit the HTML content without scrolling, and to push the non-web view content on the screen down.
The solution I came up with, was to use the _loadingFinish callback, to send the evaluation of the injectedJavaScript property back to the React Native side. In my example above, injecting 'document.getElement(elementId).offsetHeight' evaluates to the height of the element, with margins and borders applied, and once returned to the RN app, it can change the app state and cause the web view height to be adjusted.
Closes https://github.com/facebook/react-native/pull/2753
Reviewed By: svcscm
Differential Revision: D2578688
Pulled By: mkonicek
fb-gh-sync-id: fc9c0d0f84994a409e037016a555534549f8957a
2015-10-24 03:22:40 +00:00
|
|
|
_onLoadingFinish(event);
|
2015-03-14 08:22:25 +00:00
|
|
|
}
|
|
|
|
// we only need the final 'finishLoad' call so only fire the event when we're actually done loading.
|
Send the evaluation of the injectedJavaScript prop back to the React Native side
Summary: We have a use case in the development of our app, where we are rendering some HTML content in a web view. But this content comes from the back end, and varies in height. There are other components on the page, so it's not a full screen web view. We need to dynamically alter the web view height, to fit the HTML content without scrolling, and to push the non-web view content on the screen down.
The solution I came up with, was to use the _loadingFinish callback, to send the evaluation of the injectedJavaScript property back to the React Native side. In my example above, injecting 'document.getElement(elementId).offsetHeight' evaluates to the height of the element, with margins and borders applied, and once returned to the RN app, it can change the app state and cause the web view height to be adjusted.
Closes https://github.com/facebook/react-native/pull/2753
Reviewed By: svcscm
Differential Revision: D2578688
Pulled By: mkonicek
fb-gh-sync-id: fc9c0d0f84994a409e037016a555534549f8957a
2015-10-24 03:22:40 +00:00
|
|
|
else if (_onLoadingFinish && !webView.loading && ![webView.request.URL.absoluteString isEqualToString:@"about:blank"]) {
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
_onLoadingFinish([self baseEvent]);
|
2015-03-14 08:22:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|