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"
|
|
|
|
#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>
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTWebView
|
|
|
|
{
|
|
|
|
RCTEventDispatcher *_eventDispatcher;
|
|
|
|
UIWebView *_webView;
|
2015-07-08 00:07:52 +00:00
|
|
|
NSString *_injectedJavaScript;
|
2015-03-14 08:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
|
|
|
|
{
|
2015-06-15 14:53:45 +00:00
|
|
|
RCTAssertParam(eventDispatcher);
|
|
|
|
|
2015-03-14 08:22:25 +00:00
|
|
|
if ((self = [super initWithFrame:CGRectZero])) {
|
2015-04-21 01:01:46 +00:00
|
|
|
super.backgroundColor = [UIColor clearColor];
|
2015-03-14 08:22:25 +00:00
|
|
|
_automaticallyAdjustContentInsets = YES;
|
|
|
|
_contentInset = UIEdgeInsetsZero;
|
|
|
|
_eventDispatcher = eventDispatcher;
|
|
|
|
_webView = [[UIWebView alloc] initWithFrame:self.bounds];
|
|
|
|
_webView.delegate = self;
|
|
|
|
[self addSubview:_webView];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(-initWithFrame:(CGRect)frame)
|
|
|
|
RCT_NOT_IMPLEMENTED(-initWithCoder:(NSCoder *)aDecoder)
|
|
|
|
|
2015-03-14 08:22:25 +00:00
|
|
|
- (void)goForward
|
|
|
|
{
|
|
|
|
[_webView goForward];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)goBack
|
|
|
|
{
|
|
|
|
[_webView goBack];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reload
|
|
|
|
{
|
|
|
|
[_webView reload];
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (NSURL *)URL
|
|
|
|
{
|
|
|
|
return _webView.request.URL;
|
|
|
|
}
|
|
|
|
|
2015-03-14 08:22:25 +00:00
|
|
|
- (void)setURL:(NSURL *)URL
|
|
|
|
{
|
|
|
|
// 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 ([URL isEqual:_webView.request.URL]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!URL) {
|
|
|
|
// Clear the webview
|
2015-06-15 14:53:45 +00:00
|
|
|
[_webView loadHTMLString:@"" baseURL:nil];
|
2015-03-14 08:22:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
[_webView loadRequest:[NSURLRequest requestWithURL:URL]];
|
|
|
|
}
|
|
|
|
|
2015-03-31 17:05:59 +00:00
|
|
|
- (void)setHTML:(NSString *)HTML
|
|
|
|
{
|
|
|
|
[_webView loadHTMLString:HTML baseURL:nil];
|
|
|
|
}
|
|
|
|
|
2015-03-14 08:22:25 +00:00
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
|
|
|
_webView.frame = self.bounds;
|
|
|
|
[RCTView autoAdjustInsetsForView:self
|
|
|
|
withScrollView:_webView.scrollView
|
|
|
|
updateOffset:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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-03-14 08:22:25 +00:00
|
|
|
- (NSMutableDictionary *)baseEvent
|
|
|
|
{
|
|
|
|
NSURL *url = _webView.request.URL;
|
|
|
|
NSString *title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
|
|
|
|
NSMutableDictionary *event = [[NSMutableDictionary alloc] initWithDictionary: @{
|
|
|
|
@"target": self.reactTag,
|
|
|
|
@"url": url ? [url absoluteString] : @"",
|
|
|
|
@"loading" : @(_webView.loading),
|
|
|
|
@"title": title,
|
|
|
|
@"canGoBack": @([_webView canGoBack]),
|
|
|
|
@"canGoForward" : @([_webView canGoForward]),
|
|
|
|
}];
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
{
|
|
|
|
// We have this check to filter out iframe requests and whatnot
|
|
|
|
BOOL isTopFrame = [request.URL isEqual:request.mainDocumentURL];
|
|
|
|
if (isTopFrame) {
|
|
|
|
NSMutableDictionary *event = [self baseEvent];
|
|
|
|
[event addEntriesFromDictionary: @{
|
|
|
|
@"url": [request.URL absoluteString],
|
|
|
|
@"navigationType": @(navigationType)
|
|
|
|
}];
|
2015-08-11 13:37:12 +00:00
|
|
|
[_eventDispatcher sendInputEventWithName:@"loadingStart" body:event];
|
2015-03-14 08:22:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:06:39 +00:00
|
|
|
// JS Navigation handler
|
|
|
|
return ![request.URL.scheme isEqualToString:RCTJSNavigationScheme];
|
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
|
|
|
{
|
|
|
|
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 *event = [self baseEvent];
|
|
|
|
[event addEntriesFromDictionary: @{
|
|
|
|
@"domain": error.domain,
|
|
|
|
@"code": @(error.code),
|
|
|
|
@"description": [error localizedDescription],
|
|
|
|
}];
|
2015-08-11 13:37:12 +00:00
|
|
|
[_eventDispatcher sendInputEventWithName:@"loadingError" body:event];
|
2015-03-14 08:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)webViewDidFinishLoad:(UIWebView *)webView
|
|
|
|
{
|
2015-07-08 00:07:52 +00:00
|
|
|
if (_injectedJavaScript != nil) {
|
|
|
|
[webView stringByEvaluatingJavaScriptFromString:_injectedJavaScript];
|
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.
|
|
|
|
if (!webView.loading && ![webView.request.URL.absoluteString isEqualToString:@"about:blank"]) {
|
2015-08-11 13:37:12 +00:00
|
|
|
[_eventDispatcher sendInputEventWithName:@"loadingFinish" body:[self baseEvent]];
|
2015-03-14 08:22:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|