Skip to content

Commit 45705c3

Browse files
committed
Updated api to use source property like in react 0.21
1 parent d7068b8 commit 45705c3

4 files changed

Lines changed: 55 additions & 31 deletions

File tree

AQWebView.ios.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
var React = require('react-native');
2-
var { requireNativeComponent } = React;
2+
var {
3+
requireNativeComponent,
4+
PropTypes
5+
} = React;
36

47
class WKWebView extends React.Component {
58
render() {
@@ -8,20 +11,29 @@ class WKWebView extends React.Component {
811
}
912

1013
WKWebView.propTypes = {
11-
url: React.PropTypes.string,
12-
baseURL: React.PropTypes.string,
13-
html: React.PropTypes.string,
14+
source: PropTypes.oneOfType([
15+
PropTypes.shape({
16+
uri: PropTypes.string,
17+
method: PropTypes.string,
18+
headers: PropTypes.object,
19+
body: PropTypes.string,
20+
}),
21+
PropTypes.shape({
22+
html: PropTypes.string,
23+
baseUrl: PropTypes.string,
24+
}),
25+
]),
1426

15-
onLoadingStart: React.PropTypes.func,
16-
onLoadingFinish: React.PropTypes.func,
17-
onLoadingError: React.PropTypes.func,
27+
onLoadingStart: PropTypes.func,
28+
onLoadingFinish: PropTypes.func,
29+
onLoadingError: PropTypes.func,
1830

19-
automaticallyAdjustContentInsets: React.PropTypes.bool,
20-
contentInset: React.PropTypes.shape({
21-
top: React.PropTypes.number,
22-
left: React.PropTypes.number,
23-
bottom: React.PropTypes.number,
24-
right: React.PropTypes.number
31+
automaticallyAdjustContentInsets: PropTypes.bool,
32+
contentInset: PropTypes.shape({
33+
top: PropTypes.number,
34+
left: PropTypes.number,
35+
bottom: PropTypes.number,
36+
right: PropTypes.number
2537
}),
2638
};
2739

AQWebView/AQWebView.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
@interface AQWebView : RCTView
44

5-
@property (nonatomic, strong) NSURL *URL;
65
@property (nonatomic, assign) UIEdgeInsets contentInset;
76
@property (nonatomic, assign) BOOL automaticallyAdjustContentInsets;
8-
@property (nonatomic, strong) NSString *html;
9-
@property (nonatomic, assign) NSString *baseURL;
7+
@property (nonatomic, copy) NSDictionary *source;
108

119
- (void)reload;
1210

AQWebView/AQWebView.m

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#import <UIKit/UIKit.h>
33
#import "AQWebView.h"
44
#import "RCTAutoInsetsProtocol.h"
5+
#import "RCTConvert.h"
56

67
// lots of code from https://github.com/facebook/react-native/blob/master/React/Views/RCTWebView.m
78

@@ -66,19 +67,34 @@ - (void)reload
6667
[_webView loadRequest:[NSURLRequest requestWithURL:_webView.URL]];
6768
}
6869

69-
- (NSURL *)URL
70+
- (void)setSource:(NSDictionary *)source
7071
{
71-
return _webView.URL;
72-
}
73-
74-
- (void)setURL:(NSURL *)URL
75-
{
76-
[_webView loadRequest:[NSURLRequest requestWithURL:URL]];
77-
}
78-
79-
- (void)setHtml:(NSString *)html
80-
{
81-
[_webView loadHTMLString:html baseURL:[NSURL URLWithString:_baseURL]];
72+
if (![_source isEqualToDictionary:source]) {
73+
_source = [source copy];
74+
75+
// Check for a static html source first
76+
NSString *html = [RCTConvert NSString:source[@"html"]];
77+
if (html) {
78+
NSURL *baseURL = [RCTConvert NSURL:source[@"baseUrl"]];
79+
[_webView loadHTMLString:html baseURL:baseURL];
80+
return;
81+
}
82+
83+
NSURLRequest *request = [RCTConvert NSURLRequest:source];
84+
// Because of the way React works, as pages redirect, we actually end up
85+
// passing the redirect urls back here, so we ignore them if trying to load
86+
// the same url. We'll expose a call to 'reload' to allow a user to load
87+
// the existing page.
88+
if ([request.URL isEqual:_webView.URL]) {
89+
return;
90+
}
91+
if (!request.URL) {
92+
// Clear the webview
93+
[_webView loadHTMLString:@"" baseURL:nil];
94+
return;
95+
}
96+
[_webView loadRequest:request];
97+
}
8298
}
8399

84100
- (void)layoutSubviews

AQWebView/AQWebViewManager.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ - (UIView *) view
1616
return [[AQWebView alloc] init];
1717
}
1818

19-
RCT_REMAP_VIEW_PROPERTY(url, URL, NSURL);
2019
RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets);
2120
RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL);
22-
RCT_EXPORT_VIEW_PROPERTY(html, NSString*);
23-
RCT_EXPORT_VIEW_PROPERTY(baseURL, NSString*);
2421
RCT_EXPORT_VIEW_PROPERTY(onLoadingStart, RCTDirectEventBlock)
2522
RCT_EXPORT_VIEW_PROPERTY(onLoadingFinish, RCTDirectEventBlock)
2623
RCT_EXPORT_VIEW_PROPERTY(onLoadingError, RCTDirectEventBlock)
24+
RCT_EXPORT_VIEW_PROPERTY(source, NSDictionary)
2725

2826
RCT_EXPORT_METHOD(reload:(nonnull NSNumber *)reactTag)
2927
{

0 commit comments

Comments
 (0)