Commit 5a104512 cgx

处理H5页面加载服务器404、502错误

1 个父辈 a4f46392
...@@ -14,7 +14,7 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -14,7 +14,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)reloadAIPage; - (void)reloadAIPage;
@end @end
/// 加载H5页面控制器 /// 加载H5页面控制器(二级页面)
@interface DsWebController : UIViewController @interface DsWebController : UIViewController
@property (nonatomic, weak) id<DsWebControllerDelegate> refreshDelegate; @property (nonatomic, weak) id<DsWebControllerDelegate> refreshDelegate;
......
...@@ -131,14 +131,21 @@ ...@@ -131,14 +131,21 @@
self.exceptionView.hidden = YES; self.exceptionView.hidden = YES;
if (self.isShowNavi == NO) { self.dkBackBtn.hidden = YES; } if (self.isShowNavi == NO) { self.dkBackBtn.hidden = YES; }
[self.dsMaskView display]; [self.dsMaskView display];
self.webView.hidden = NO; self.webView.hidden = !self.exceptionView.hidden;
[self serverErrorDeal];
} }
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error { - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
DSLog(@"加载失败:%@", error.userInfo); DSLog(@"加载失败:%@", error.userInfo);
self.exceptionView.hidden = NO; [self wkErrorDeal];
if (self.isShowNavi == NO) { self.dkBackBtn.hidden = NO; } }
// 提交发生错误时调用
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
DSLog(@"提交失败:%@", error.userInfo);
[self wkErrorDeal];
} }
// 内存不足出现白屏问题 // 内存不足出现白屏问题
...@@ -150,6 +157,32 @@ ...@@ -150,6 +157,32 @@
[DataStatisticsUtil reportExceptionWithName:H5Monitor reason:@"webViewWebContentProcessDidTerminate" stackTrace:@[url]]; [DataStatisticsUtil reportExceptionWithName:H5Monitor reason:@"webViewWebContentProcessDidTerminate" stackTrace:@[url]];
} }
#pragma mark - 错误处理
- (void)wkErrorDeal {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
self.exceptionView.hidden = NO;
self.webView.hidden = !self.exceptionView.hidden;
if (self.isShowNavi == NO) { self.dkBackBtn.hidden = NO; }
}
- (void)serverErrorDeal {
// 网页加载成功了,处理服务器常见错误
NSURLSessionTask * datatask = [[NSURLSession sharedSession] dataTaskWithRequest:self.request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
DSLog(@"statusCode:%ld, error:%@, info:%@", httpResponse.statusCode, error, [NSHTTPURLResponse localizedStringForStatusCode:httpResponse.statusCode]);
if (httpResponse.statusCode == 404 || httpResponse.statusCode == 502) {
dispatch_async(dispatch_get_main_queue(), ^{
self.exceptionView.hidden = NO;
if (self.isShowNavi == NO) { self.dkBackBtn.hidden = NO; }
self.webView.hidden = !self.exceptionView.hidden;
[self.exceptionView showServerErrInfo:[NSHTTPURLResponse localizedStringForStatusCode:httpResponse.statusCode]];
});
}
}];
[datatask resume];
}
#pragma mark - WKScriptMessageHandler #pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([message.name isEqualToString:@"AppModel"]) { if ([message.name isEqualToString:@"AppModel"]) {
......
...@@ -11,8 +11,9 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -11,8 +11,9 @@ NS_ASSUME_NONNULL_BEGIN
// 异常处理类型 // 异常处理类型
typedef NS_ENUM(NSInteger, ExceptionType) { typedef NS_ENUM(NSInteger, ExceptionType) {
ExceptionTypeNet, ExceptionTypeNet, // 网络异常
ExceptionTypeSmall ExceptionTypeSmall, // 小页面网络异常
ExceptionTypeNoData // 无数据
}; };
// 处理事件回调 // 处理事件回调
...@@ -23,6 +24,10 @@ typedef void (^DealBlock)(void); ...@@ -23,6 +24,10 @@ typedef void (^DealBlock)(void);
- (instancetype)initWithType:(ExceptionType)type block:(DealBlock)block superView:(UIView *)superView; - (instancetype)initWithType:(ExceptionType)type block:(DealBlock)block superView:(UIView *)superView;
- (void)updateExceptionViewWithType:(ExceptionType)type;
- (void)showServerErrInfo:(NSString *)errInfo;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END
...@@ -28,49 +28,59 @@ ...@@ -28,49 +28,59 @@
[self addSubview:self.exceptLab]; [self addSubview:self.exceptLab];
[self addSubview:self.dealBtn]; [self addSubview:self.dealBtn];
if (type == ExceptionTypeNet) { if (type == ExceptionTypeSmall) {
[self addSubview:self.exceptionIV];
[self mas_makeConstraints:^(MASConstraintMaker *make) { [self mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(superView); make.top.left.bottom.right.equalTo(superView);
make.center.equalTo(superView);
}];
[self.exceptionIV mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.top.equalTo(self);
}]; }];
[self.exceptLab mas_makeConstraints:^(MASConstraintMaker *make) { [self.exceptLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self); make.centerX.equalTo(self);
make.left.equalTo(self).offset(5); make.left.equalTo(self).offset(5);
make.right.equalTo(self).offset(-5); make.right.equalTo(self).offset(-5);
make.top.equalTo(self.exceptionIV.mas_bottom).offset(42); make.centerY.equalTo(self).offset(-30);
}]; }];
[self.dealBtn mas_makeConstraints:^(MASConstraintMaker *make) { [self.dealBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self); make.centerX.equalTo(self);
make.size.mas_equalTo(CGSizeMake(155, 40)); make.size.mas_equalTo(CGSizeMake(155, 40));
make.top.equalTo(self.exceptLab.mas_bottom).offset(42); make.centerY.equalTo(self).offset(30);
make.bottom.equalTo(self).offset(-30);
}]; }];
} else if (type == ExceptionTypeSmall) { } else {
[self addSubview:self.exceptionIV];
[self mas_makeConstraints:^(MASConstraintMaker *make) { [self mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.bottom.right.equalTo(superView); make.left.right.equalTo(superView);
make.center.equalTo(superView);
}];
[self.exceptionIV mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.top.equalTo(self);
}]; }];
[self.exceptLab mas_makeConstraints:^(MASConstraintMaker *make) { [self.exceptLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self); make.centerX.equalTo(self);
make.left.equalTo(self).offset(5); make.left.equalTo(self).offset(5);
make.right.equalTo(self).offset(-5); make.right.equalTo(self).offset(-5);
make.centerY.equalTo(self).offset(-30); make.top.equalTo(self.exceptionIV.mas_bottom).offset(42);
}]; }];
[self.dealBtn mas_makeConstraints:^(MASConstraintMaker *make) { [self.dealBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self); make.centerX.equalTo(self);
make.size.mas_equalTo(CGSizeMake(155, 40)); make.size.mas_equalTo(CGSizeMake(155, 40));
make.centerY.equalTo(self).offset(30); make.top.equalTo(self.exceptLab.mas_bottom).offset(42);
make.bottom.equalTo(self).offset(-30);
}]; }];
} }
} }
return self; return self;
} }
- (void)updateExceptionViewWithType:(ExceptionType)type {
self.exceptionIV.image = [UIImage imageNamed:[self getExceptionImgWithType:type]];
self.exceptLab.text = [self getExceptionInfoWithType:type];
[self.dealBtn setTitle:[self getDealBtnTitleWithType:self.type] forState:UIControlStateNormal];
}
- (void)showServerErrInfo:(NSString *)errInfo {
self.exceptLab.text = (errInfo && [errInfo isKindOfClass:[NSString class]]) ? errInfo : @"服务器异常";
}
#pragma mark - Actions #pragma mark - Actions
- (void)dealAction { - (void)dealAction {
if (self.dealBlock) { self.dealBlock(); } if (self.dealBlock) { self.dealBlock(); }
...@@ -79,7 +89,7 @@ ...@@ -79,7 +89,7 @@
#pragma mark - lazy #pragma mark - lazy
- (UIImageView *)exceptionIV { - (UIImageView *)exceptionIV {
if (!_exceptionIV) { if (!_exceptionIV) {
_exceptionIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"netDefault"]]; _exceptionIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self getExceptionImgWithType:self.type]]];
} }
return _exceptionIV; return _exceptionIV;
} }
...@@ -103,12 +113,26 @@ ...@@ -103,12 +113,26 @@
} }
#pragma mark - private #pragma mark - private
- (NSString *)getExceptionImgWithType:(ExceptionType)type {
NSString *imgName = @"netDefault";
if (type == ExceptionTypeNet) {
imgName = @"netDefault";
} else if (type == ExceptionTypeSmall) {
imgName = @"netDefault";
} else if (type == ExceptionTypeNoData) {
imgName = @"noData";
}
return imgName;
}
- (NSString *)getExceptionInfoWithType:(ExceptionType)type { - (NSString *)getExceptionInfoWithType:(ExceptionType)type {
NSString *info = @""; NSString *info = @"";
if (type == ExceptionTypeNet) { if (type == ExceptionTypeNet) {
info = @"当前网络环境较差,点击刷新重新加载~"; info = @"当前网络环境较差,点击刷新重新加载~";
} else if (type == ExceptionTypeSmall) { } else if (type == ExceptionTypeSmall) {
info = @"当前网络环境较差,点击刷新重新加载~"; info = @"当前网络环境较差,点击刷新重新加载~";
} else if (type == ExceptionTypeNoData) {
info = @"空空如也哦~";
} }
return info; return info;
} }
...@@ -119,6 +143,8 @@ ...@@ -119,6 +143,8 @@
title = @"刷新"; title = @"刷新";
} else if (type == ExceptionTypeSmall) { } else if (type == ExceptionTypeSmall) {
title = @"刷新"; title = @"刷新";
} else if (type == ExceptionTypeNoData) {
title = @"重新获取";
} }
return title; return title;
} }
......
...@@ -120,15 +120,21 @@ ...@@ -120,15 +120,21 @@
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
self.exceptionView.hidden = YES; self.exceptionView.hidden = YES;
self.aiWebView.hidden = NO; self.aiWebView.hidden = !self.exceptionView.hidden;
[self serverErrorDeal];
} }
// 加载失败 // 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error { - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; DSLog(@"加载失败:%@", error.userInfo);
self.exceptionView.hidden = NO; [self wkErrorDeal];
self.aiWebView.hidden = YES; }
DSLog(@"加载失败:%@", error);
// 提交发生错误时调用
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
DSLog(@"提交失败:%@", error.userInfo);
[self wkErrorDeal];
} }
// 内存不足出现白屏问题 // 内存不足出现白屏问题
...@@ -140,6 +146,30 @@ ...@@ -140,6 +146,30 @@
[DataStatisticsUtil reportExceptionWithName:H5Monitor reason:@"webViewWebContentProcessDidTerminate" stackTrace:@[url]]; [DataStatisticsUtil reportExceptionWithName:H5Monitor reason:@"webViewWebContentProcessDidTerminate" stackTrace:@[url]];
} }
#pragma mark - 错误处理
- (void)wkErrorDeal {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
self.exceptionView.hidden = NO;
self.aiWebView.hidden = !self.exceptionView.hidden;
}
- (void)serverErrorDeal {
// 网页加载成功了,处理服务器常见错误
NSURLSessionTask * datatask = [[NSURLSession sharedSession] dataTaskWithRequest:self.request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
DSLog(@"statusCode:%ld, error:%@, info:%@", httpResponse.statusCode, error, [NSHTTPURLResponse localizedStringForStatusCode:httpResponse.statusCode]);
if (httpResponse.statusCode == 404 || httpResponse.statusCode == 502) {
dispatch_async(dispatch_get_main_queue(), ^{
self.exceptionView.hidden = NO;
self.aiWebView.hidden = !self.exceptionView.hidden;
[self.exceptionView showServerErrInfo:[NSHTTPURLResponse localizedStringForStatusCode:httpResponse.statusCode]];
});
}
}];
[datatask resume];
}
#pragma mark - WKScriptMessageHandler #pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([message.name isEqualToString:@"AppModel"]) { if ([message.name isEqualToString:@"AppModel"]) {
......
{
"images" : [
{
"filename" : "noData.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "noData@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "noData@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!