Skip to content

Commit d03ec32

Browse files
committed
Added callbacks
1 parent 7a33d46 commit d03ec32

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

AQWebView.ios.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ WKWebView.propTypes = {
1111
url: React.PropTypes.string,
1212
baseURL: React.PropTypes.string,
1313
html: React.PropTypes.string,
14-
14+
15+
onLoadingStart: React.PropTypes.func,
16+
onLoadingFinish: React.PropTypes.func,
17+
onLoadingError: React.PropTypes.func,
18+
1519
automaticallyAdjustContentInsets: React.PropTypes.bool,
1620
contentInset: React.PropTypes.shape({
1721
top: React.PropTypes.number,

AQWebView/AQWebView.m

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// lots of code from https://github.com/facebook/react-native/blob/master/React/Views/RCTWebView.m
77

88
@interface AQWebView () <RCTAutoInsetsProtocol, WKNavigationDelegate>
9+
@property (nonatomic, copy) RCTDirectEventBlock onLoadingStart;
10+
@property (nonatomic, copy) RCTDirectEventBlock onLoadingFinish;
11+
@property (nonatomic, copy) RCTDirectEventBlock onLoadingError;
912
@end
1013

1114
@implementation AQWebView
@@ -109,12 +112,56 @@ - (void)refreshContentInset
109112
updateOffset:YES];
110113
}
111114

115+
- (NSMutableDictionary<NSString *, id> *)baseEvent
116+
{
117+
NSMutableDictionary<NSString *, id> *event = [[NSMutableDictionary alloc] initWithDictionary:
118+
@{
119+
@"url": [_webView.URL absoluteString] ?: @"",
120+
@"loading" : @(_webView.loading),
121+
@"title": [_webView title] ?: @"",
122+
@"canGoBack": @(_webView.canGoBack),
123+
@"canGoForward" : @(_webView.canGoForward),
124+
}
125+
];
126+
127+
return event;
128+
}
129+
112130
#pragma mark - WKNavigationDelegate
113131

114132
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
115133
{
116134
[_spinner stopAnimating];
117135
[_refreshControl endRefreshing];
136+
137+
if (_onLoadingFinish) {
138+
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
139+
_onLoadingFinish(event);
140+
}
141+
}
142+
143+
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
144+
{
145+
if (_onLoadingStart) {
146+
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
147+
_onLoadingStart(event);
148+
}
149+
}
150+
151+
-(void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error
152+
{
153+
[_spinner stopAnimating];
154+
[_refreshControl endRefreshing];
155+
156+
if (_onLoadingError) {
157+
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
158+
[event addEntriesFromDictionary:@{
159+
@"domain": error.domain,
160+
@"code": @(error.code),
161+
@"description": error.localizedDescription,
162+
}];
163+
_onLoadingError(event);
164+
}
118165
}
119166

120167
@end

AQWebView/AQWebViewManager.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ - (UIView *) view
1919
RCT_REMAP_VIEW_PROPERTY(url, URL, NSURL);
2020
RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets);
2121
RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL);
22-
2322
RCT_EXPORT_VIEW_PROPERTY(html, NSString*);
2423
RCT_EXPORT_VIEW_PROPERTY(baseURL, NSString*);
24+
RCT_EXPORT_VIEW_PROPERTY(onLoadingStart, RCTDirectEventBlock)
25+
RCT_EXPORT_VIEW_PROPERTY(onLoadingFinish, RCTDirectEventBlock)
26+
RCT_EXPORT_VIEW_PROPERTY(onLoadingError, RCTDirectEventBlock)
2527

2628
RCT_EXPORT_METHOD(reload:(nonnull NSNumber *)reactTag)
2729
{

0 commit comments

Comments
 (0)