-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRNBridgeableWebView.m
218 lines (187 loc) · 6.97 KB
/
RNBridgeableWebView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* 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.
*/
#import "RNBridgeableWebView.h"
#import <UIKit/UIKit.h>
#import "RCTAutoInsetsProtocol.h"
#import "RCTEventDispatcher.h"
#import "RCTLog.h"
#import "RCTUtils.h"
#import "RCTView.h"
#import "UIView+React.h"
@interface RNBridgeableWebView () <UIWebViewDelegate, RCTAutoInsetsProtocol>
@end
NSString *const RNBridgeableWebViewMessageSent = @"messageSent";
@implementation RNBridgeableWebView
{
RCTEventDispatcher *_eventDispatcher;
UIWebView *_webView;
}
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
{
if ((self = [super initWithFrame:CGRectZero])) {
super.backgroundColor = [UIColor clearColor];
_automaticallyAdjustContentInsets = YES;
_contentInset = UIEdgeInsetsZero;
_eventDispatcher = eventDispatcher;
_webView = [[UIWebView alloc] initWithFrame:self.bounds];
_webView.delegate = self;
[self addSubview:_webView];
}
return self;
}
- (void)goForward
{
[_webView goForward];
}
- (void)goBack
{
[_webView goBack];
}
- (void)reload
{
[_webView reload];
}
- (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
[_webView loadHTMLString:nil baseURL:nil];
return;
}
[_webView loadRequest:[NSURLRequest requestWithURL:URL]];
}
- (void)setHTML:(NSString *)HTML
{
[_webView loadHTMLString:HTML baseURL:nil];
}
- (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];
}
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
CGFloat alpha = CGColorGetAlpha(backgroundColor.CGColor);
self.opaque = _webView.opaque = (alpha == 1.0);
_webView.backgroundColor = backgroundColor;
}
- (UIColor *)backgroundColor
{
return _webView.backgroundColor;
}
- (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
static NSString *const RCTJSAJAXScheme = @"react-ajax";
static NSString *const RNBridgeScheme = @"react-message";
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.scheme isEqualToString:RNBridgeScheme]) {
NSMutableDictionary *event = [self baseEvent];
[event addEntriesFromDictionary: @{
@"url": [request.URL absoluteString],
@"navigationType": @(navigationType)
}];
[_eventDispatcher sendInputEventWithName:RNBridgeableWebViewMessageSent body:event];
return false;
}
// 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)
}];
[_eventDispatcher sendInputEventWithName:@"topLoadingStart" body:event];
}
// AJAX handler
return ![request.URL.scheme isEqualToString:RCTJSAJAXScheme];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
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],
}];
[_eventDispatcher sendInputEventWithName:@"topLoadingError" body:event];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if (_shouldInjectAJAXHandler) {
// From http://stackoverflow.com/questions/5353278/uiwebviewdelegate-not-monitoring-xmlhttprequest
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"\
var s_ajaxListener = new Object(); \n\
s_ajaxListener.tempOpen = XMLHttpRequest.prototype.open; \n\
s_ajaxListener.tempSend = XMLHttpRequest.prototype.send; \n\
s_ajaxListener.callback = function() { \n\
window.location.href = '%@://' + this.url; \n\
} \n\
XMLHttpRequest.prototype.open = function(a,b) { \n\
s_ajaxListener.tempOpen.apply(this, arguments); \n\
s_ajaxListener.method = a; \n\
s_ajaxListener.url = b; \n\
if (a.toLowerCase() === 'get') { \n\
s_ajaxListener.data = (b.split('?'))[1]; \n\
} \n\
} \n\
XMLHttpRequest.prototype.send = function(a,b) { \n\
s_ajaxListener.tempSend.apply(this, arguments); \n\
if (s_ajaxListener.method.toLowerCase() === 'post') { \n\
s_ajaxListener.data = a; \n\
} \n\
s_ajaxListener.callback(); \n\
} \n\
", RCTJSAJAXScheme]];
}
// 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"]) {
[_eventDispatcher sendInputEventWithName:@"topLoadingFinish" body:[self baseEvent]];
}
}
@end