|
6 | 6 | // lots of code from https://github.com/facebook/react-native/blob/master/React/Views/RCTWebView.m |
7 | 7 |
|
8 | 8 | @interface AQWebView () <RCTAutoInsetsProtocol, WKNavigationDelegate> |
| 9 | +@property (nonatomic, copy) RCTDirectEventBlock onLoadingStart; |
| 10 | +@property (nonatomic, copy) RCTDirectEventBlock onLoadingFinish; |
| 11 | +@property (nonatomic, copy) RCTDirectEventBlock onLoadingError; |
9 | 12 | @end |
10 | 13 |
|
11 | 14 | @implementation AQWebView |
@@ -109,12 +112,56 @@ - (void)refreshContentInset |
109 | 112 | updateOffset:YES]; |
110 | 113 | } |
111 | 114 |
|
| 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 | + |
112 | 130 | #pragma mark - WKNavigationDelegate |
113 | 131 |
|
114 | 132 | -(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation |
115 | 133 | { |
116 | 134 | [_spinner stopAnimating]; |
117 | 135 | [_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 | + } |
118 | 165 | } |
119 | 166 |
|
120 | 167 | @end |
0 commit comments