webview加载失败 刷新 机制 修改
review by 枪兵
Showing
2 changed files
with
37 additions
and
7 deletions
@@ -34,6 +34,8 @@ static float kprogressViewRadius = 2.0f; | @@ -34,6 +34,8 @@ static float kprogressViewRadius = 2.0f; | ||
34 | 34 | ||
35 | @property (strong, nonatomic) UIButton *retryButton; //重试按钮 | 35 | @property (strong, nonatomic) UIButton *retryButton; //重试按钮 |
36 | 36 | ||
37 | +@property (strong, nonatomic) NSURL *failURL; //只有当走失败回调时会有失败url | ||
38 | + | ||
37 | @end | 39 | @end |
38 | 40 | ||
39 | @implementation YHExplorerViewController | 41 | @implementation YHExplorerViewController |
@@ -318,9 +320,7 @@ static float kprogressViewRadius = 2.0f; | @@ -318,9 +320,7 @@ static float kprogressViewRadius = 2.0f; | ||
318 | { | 320 | { |
319 | [[YHInjectConfigManager sharedInstance] cycleInject:theWebView]; | 321 | [[YHInjectConfigManager sharedInstance] cycleInject:theWebView]; |
320 | 322 | ||
321 | - if (self.originRequest == nil) { | ||
322 | - self.originRequest = theWebView.request; | ||
323 | - } | 323 | + self.originRequest = theWebView.request; |
324 | 324 | ||
325 | NSString *title = [theWebView stringByEvaluatingJavaScriptFromString: @"document.title"]; | 325 | NSString *title = [theWebView stringByEvaluatingJavaScriptFromString: @"document.title"]; |
326 | _pageTitle = title; | 326 | _pageTitle = title; |
@@ -333,7 +333,6 @@ static float kprogressViewRadius = 2.0f; | @@ -333,7 +333,6 @@ static float kprogressViewRadius = 2.0f; | ||
333 | 333 | ||
334 | NSString *key = [[NSString alloc] initWithFormat:@"WebKit%@%@", @"CacheModel", @"PreferenceKey"];//WebKitCacheModelPreferenceKey | 334 | NSString *key = [[NSString alloc] initWithFormat:@"WebKit%@%@", @"CacheModel", @"PreferenceKey"];//WebKitCacheModelPreferenceKey |
335 | [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:key]; | 335 | [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:key]; |
336 | - [[NSUserDefaults standardUserDefaults] synchronize]; | ||
337 | } | 336 | } |
338 | 337 | ||
339 | - (void)webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error | 338 | - (void)webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error |
@@ -369,6 +368,8 @@ static float kprogressViewRadius = 2.0f; | @@ -369,6 +368,8 @@ static float kprogressViewRadius = 2.0f; | ||
369 | // 页面加载完成之后调用 2 | 368 | // 页面加载完成之后调用 2 |
370 | - (void)webView:(WKWebView*)webView didFinishNavigation:(WKNavigation*)navigation | 369 | - (void)webView:(WKWebView*)webView didFinishNavigation:(WKNavigation*)navigation |
371 | { | 370 | { |
371 | + self.originRequest = [NSURLRequest requestWithURL:webView.URL]; | ||
372 | + | ||
372 | [self callback_webViewDidFinishLoad]; | 373 | [self callback_webViewDidFinishLoad]; |
373 | } | 374 | } |
374 | // 页面加载失败时调用 | 375 | // 页面加载失败时调用 |
@@ -489,6 +490,7 @@ static float kprogressViewRadius = 2.0f; | @@ -489,6 +490,7 @@ static float kprogressViewRadius = 2.0f; | ||
489 | 490 | ||
490 | - (void)callback_webViewDidFinishLoad | 491 | - (void)callback_webViewDidFinishLoad |
491 | { | 492 | { |
493 | + self.failURL = nil; | ||
492 | if ([self.progressBar respondsToSelector:@selector(yhExplorer_webViewDidFinishLoad:)]) { | 494 | if ([self.progressBar respondsToSelector:@selector(yhExplorer_webViewDidFinishLoad:)]) { |
493 | [self.progressBar yhExplorer_webViewDidFinishLoad:self]; | 495 | [self.progressBar yhExplorer_webViewDidFinishLoad:self]; |
494 | } | 496 | } |
@@ -501,8 +503,26 @@ static float kprogressViewRadius = 2.0f; | @@ -501,8 +503,26 @@ static float kprogressViewRadius = 2.0f; | ||
501 | 503 | ||
502 | - (void)callback_webViewDidFailLoadWithError:(NSError *)error | 504 | - (void)callback_webViewDidFailLoadWithError:(NSError *)error |
503 | { | 505 | { |
504 | - //TODO:错误描述信息 | ||
505 | - [self addRetryButton]; | 506 | + NSString *failingURLString = [error.userInfo objectForKey:NSURLErrorFailingURLStringErrorKey]; |
507 | + failingURLString = [NSString stringWithFormat:@"%@", failingURLString]; | ||
508 | + | ||
509 | + if (error.code == -1009) { | ||
510 | + self.failURL = [NSURL URLWithString:failingURLString]; | ||
511 | + [self addRetryButton]; | ||
512 | + } else { | ||
513 | + self.failURL = nil; | ||
514 | + NSString *failDescription = [error.userInfo objectForKey:NSLocalizedDescriptionKey]; | ||
515 | + NSMutableDictionary *errorDict = @{}.mutableCopy; | ||
516 | + [errorDict setValue:@(error.code) forKey:@"Code"]; | ||
517 | + [errorDict setValue:failDescription forKey:@"Description"]; | ||
518 | + [errorDict setValue:failingURLString forKey:@"URL"]; | ||
519 | + NSString *message = [NSString stringWithFormat:@"%@", errorDict]; | ||
520 | + UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"ERROR" message:message preferredStyle:UIAlertControllerStyleAlert]; | ||
521 | + [alertController addAction:[UIAlertAction actionWithTitle:@"确认" | ||
522 | + style:UIAlertActionStyleCancel | ||
523 | + handler:nil]]; | ||
524 | + [self presentViewController:alertController animated:YES completion:nil]; | ||
525 | + } | ||
506 | if ([self.progressBar respondsToSelector:@selector(yhExplorer_webView:didFailLoadWithError:)]) | 526 | if ([self.progressBar respondsToSelector:@selector(yhExplorer_webView:didFailLoadWithError:)]) |
507 | { | 527 | { |
508 | [self.progressBar yhExplorer_webView:self didFailLoadWithError:error]; | 528 | [self.progressBar yhExplorer_webView:self didFailLoadWithError:error]; |
@@ -836,6 +856,7 @@ static float kprogressViewRadius = 2.0f; | @@ -836,6 +856,7 @@ static float kprogressViewRadius = 2.0f; | ||
836 | if (!_retryButton) { | 856 | if (!_retryButton) { |
837 | _retryButton = [UIButton buttonWithType:UIButtonTypeCustom]; | 857 | _retryButton = [UIButton buttonWithType:UIButtonTypeCustom]; |
838 | _retryButton.frame = self.webView.bounds; | 858 | _retryButton.frame = self.webView.bounds; |
859 | + _retryButton.backgroundColor = UIColor.whiteColor; | ||
839 | [_retryButton setImage:[UIImage imageNamed:@"yh_explorer_refresh_icon"] forState:UIControlStateNormal]; | 860 | [_retryButton setImage:[UIImage imageNamed:@"yh_explorer_refresh_icon"] forState:UIControlStateNormal]; |
840 | [_retryButton setImage:[UIImage imageNamed:@"yh_explorer_refresh_icon"] forState:UIControlStateHighlighted]; | 861 | [_retryButton setImage:[UIImage imageNamed:@"yh_explorer_refresh_icon"] forState:UIControlStateHighlighted]; |
841 | [_retryButton addTarget:self action:@selector(retryAction:) forControlEvents:UIControlEventTouchUpInside]; | 862 | [_retryButton addTarget:self action:@selector(retryAction:) forControlEvents:UIControlEventTouchUpInside]; |
@@ -846,8 +867,15 @@ static float kprogressViewRadius = 2.0f; | @@ -846,8 +867,15 @@ static float kprogressViewRadius = 2.0f; | ||
846 | - (void)retryAction:(id)sender | 867 | - (void)retryAction:(id)sender |
847 | { | 868 | { |
848 | if (!self.isLoading) { | 869 | if (!self.isLoading) { |
849 | - [self reloadFromOrigin]; | 870 | + if (self.failURL) { |
871 | + [self.webViewEngine loadRequest:[NSURLRequest requestWithURL:self.failURL]]; | ||
872 | + } else { | ||
873 | + if (![self checkAndReinitViewUrl]) { | ||
874 | + [self reloadFromOrigin]; | ||
875 | + } | ||
876 | + } | ||
850 | } | 877 | } |
878 | + self.failURL = nil; | ||
851 | } | 879 | } |
852 | 880 | ||
853 | - (void)addRetryButton | 881 | - (void)addRetryButton |
-
Please register or login to post a comment