Authored by 孟顺

webview加载失败 刷新 机制 修改

review by 枪兵
... ... @@ -89,4 +89,6 @@
- (void)parseSettingsWithParser:(NSObject <NSXMLParserDelegate>*)delegate;
- (bool)checkAndReinitViewUrl;
@end
... ...
... ... @@ -34,6 +34,8 @@ static float kprogressViewRadius = 2.0f;
@property (strong, nonatomic) UIButton *retryButton; //重试按钮
@property (strong, nonatomic) NSURL *failURL; //只有当走失败回调时会有失败url
@end
@implementation YHExplorerViewController
... ... @@ -318,9 +320,7 @@ static float kprogressViewRadius = 2.0f;
{
[[YHInjectConfigManager sharedInstance] cycleInject:theWebView];
if (self.originRequest == nil) {
self.originRequest = theWebView.request;
}
NSString *title = [theWebView stringByEvaluatingJavaScriptFromString: @"document.title"];
_pageTitle = title;
... ... @@ -333,7 +333,6 @@ static float kprogressViewRadius = 2.0f;
NSString *key = [[NSString alloc] initWithFormat:@"WebKit%@%@", @"CacheModel", @"PreferenceKey"];//WebKitCacheModelPreferenceKey
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error
... ... @@ -369,6 +368,8 @@ static float kprogressViewRadius = 2.0f;
// 页面加载完成之后调用 2
- (void)webView:(WKWebView*)webView didFinishNavigation:(WKNavigation*)navigation
{
self.originRequest = [NSURLRequest requestWithURL:webView.URL];
[self callback_webViewDidFinishLoad];
}
// 页面加载失败时调用
... ... @@ -489,6 +490,7 @@ static float kprogressViewRadius = 2.0f;
- (void)callback_webViewDidFinishLoad
{
self.failURL = nil;
if ([self.progressBar respondsToSelector:@selector(yhExplorer_webViewDidFinishLoad:)]) {
[self.progressBar yhExplorer_webViewDidFinishLoad:self];
}
... ... @@ -501,8 +503,26 @@ static float kprogressViewRadius = 2.0f;
- (void)callback_webViewDidFailLoadWithError:(NSError *)error
{
//TODO:错误描述信息
NSString *failingURLString = [error.userInfo objectForKey:NSURLErrorFailingURLStringErrorKey];
failingURLString = [NSString stringWithFormat:@"%@", failingURLString];
if (error.code == -1009) {
self.failURL = [NSURL URLWithString:failingURLString];
[self addRetryButton];
} else {
self.failURL = nil;
NSString *failDescription = [error.userInfo objectForKey:NSLocalizedDescriptionKey];
NSMutableDictionary *errorDict = @{}.mutableCopy;
[errorDict setValue:@(error.code) forKey:@"Code"];
[errorDict setValue:failDescription forKey:@"Description"];
[errorDict setValue:failingURLString forKey:@"URL"];
NSString *message = [NSString stringWithFormat:@"%@", errorDict];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"ERROR" message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"确认"
style:UIAlertActionStyleCancel
handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
}
if ([self.progressBar respondsToSelector:@selector(yhExplorer_webView:didFailLoadWithError:)])
{
[self.progressBar yhExplorer_webView:self didFailLoadWithError:error];
... ... @@ -836,6 +856,7 @@ static float kprogressViewRadius = 2.0f;
if (!_retryButton) {
_retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
_retryButton.frame = self.webView.bounds;
_retryButton.backgroundColor = UIColor.whiteColor;
[_retryButton setImage:[UIImage imageNamed:@"yh_explorer_refresh_icon"] forState:UIControlStateNormal];
[_retryButton setImage:[UIImage imageNamed:@"yh_explorer_refresh_icon"] forState:UIControlStateHighlighted];
[_retryButton addTarget:self action:@selector(retryAction:) forControlEvents:UIControlEventTouchUpInside];
... ... @@ -846,8 +867,15 @@ static float kprogressViewRadius = 2.0f;
- (void)retryAction:(id)sender
{
if (!self.isLoading) {
if (self.failURL) {
[self.webViewEngine loadRequest:[NSURLRequest requestWithURL:self.failURL]];
} else {
if (![self checkAndReinitViewUrl]) {
[self reloadFromOrigin];
}
}
}
self.failURL = nil;
}
- (void)addRetryButton
... ...