Skip to content

Commit 80ee964

Browse files
floorishcharafau
authored andcommitted
Fixed error methods on iOS (#551)
1 parent 06e7528 commit 80ee964

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

ios/Classes/FlutterWebviewPlugin.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,19 +358,27 @@ - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation
358358
[channel invokeMethod:@"onState" arguments:@{@"type": @"startLoad", @"url": webView.URL.absoluteString}];
359359
}
360360

361+
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
362+
NSString* url = webView.URL == nil ? @"?" : webView.URL.absoluteString;
363+
364+
[channel invokeMethod:@"onHttpError" arguments:@{@"code": [NSString stringWithFormat:@"%ld", error.code], @"url": url}];
365+
}
366+
361367
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
362368
[channel invokeMethod:@"onState" arguments:@{@"type": @"finishLoad", @"url": webView.URL.absoluteString}];
363369
}
364370

365371
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
366-
[channel invokeMethod:@"onError" arguments:@{@"code": [NSString stringWithFormat:@"%ld", error.code], @"error": error.localizedDescription}];
372+
[channel invokeMethod:@"onHttpError" arguments:@{@"code": [NSString stringWithFormat:@"%ld", error.code], @"error": error.localizedDescription}];
367373
}
368374

369375
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
370376
if ([navigationResponse.response isKindOfClass:[NSHTTPURLResponse class]]) {
371377
NSHTTPURLResponse * response = (NSHTTPURLResponse *)navigationResponse.response;
372378

373-
[channel invokeMethod:@"onHttpError" arguments:@{@"code": [NSString stringWithFormat:@"%ld", response.statusCode], @"url": webView.URL.absoluteString}];
379+
if (response.statusCode >= 400) {
380+
[channel invokeMethod:@"onHttpError" arguments:@{@"code": [NSString stringWithFormat:@"%ld", response.statusCode], @"url": webView.URL.absoluteString}];
381+
}
374382
}
375383
decisionHandler(WKNavigationResponsePolicyAllow);
376384
}

0 commit comments

Comments
 (0)