Commit 5a104512 cgx

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

1 个父辈 a4f46392
......@@ -14,7 +14,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)reloadAIPage;
@end
/// 加载H5页面控制器
/// 加载H5页面控制器(二级页面)
@interface DsWebController : UIViewController
@property (nonatomic, weak) id<DsWebControllerDelegate> refreshDelegate;
......
......@@ -131,14 +131,21 @@
self.exceptionView.hidden = YES;
if (self.isShowNavi == NO) { self.dkBackBtn.hidden = YES; }
[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 {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
DSLog(@"加载失败:%@", error.userInfo);
self.exceptionView.hidden = NO;
if (self.isShowNavi == NO) { self.dkBackBtn.hidden = NO; }
[self wkErrorDeal];
}
// 提交发生错误时调用
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
DSLog(@"提交失败:%@", error.userInfo);
[self wkErrorDeal];
}
// 内存不足出现白屏问题
......@@ -150,6 +157,32 @@
[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
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([message.name isEqualToString:@"AppModel"]) {
......
......@@ -11,8 +11,9 @@ NS_ASSUME_NONNULL_BEGIN
// 异常处理类型
typedef NS_ENUM(NSInteger, ExceptionType) {
ExceptionTypeNet,
ExceptionTypeSmall
ExceptionTypeNet, // 网络异常
ExceptionTypeSmall, // 小页面网络异常
ExceptionTypeNoData // 无数据
};
// 处理事件回调
......@@ -23,6 +24,10 @@ typedef void (^DealBlock)(void);
- (instancetype)initWithType:(ExceptionType)type block:(DealBlock)block superView:(UIView *)superView;
- (void)updateExceptionViewWithType:(ExceptionType)type;
- (void)showServerErrInfo:(NSString *)errInfo;
@end
NS_ASSUME_NONNULL_END
......@@ -28,49 +28,59 @@
[self addSubview:self.exceptLab];
[self addSubview:self.dealBtn];
if (type == ExceptionTypeNet) {
[self addSubview:self.exceptionIV];
if (type == ExceptionTypeSmall) {
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(superView);
make.center.equalTo(superView);
}];
[self.exceptionIV mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.top.equalTo(self);
make.top.left.bottom.right.equalTo(superView);
}];
[self.exceptLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.left.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) {
make.centerX.equalTo(self);
make.size.mas_equalTo(CGSizeMake(155, 40));
make.top.equalTo(self.exceptLab.mas_bottom).offset(42);
make.bottom.equalTo(self).offset(-30);
make.centerY.equalTo(self).offset(30);
}];
} else if (type == ExceptionTypeSmall) {
} else {
[self addSubview:self.exceptionIV];
[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) {
make.centerX.equalTo(self);
make.left.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) {
make.centerX.equalTo(self);
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;
}
- (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
- (void)dealAction {
if (self.dealBlock) { self.dealBlock(); }
......@@ -79,7 +89,7 @@
#pragma mark - lazy
- (UIImageView *)exceptionIV {
if (!_exceptionIV) {
_exceptionIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"netDefault"]];
_exceptionIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self getExceptionImgWithType:self.type]]];
}
return _exceptionIV;
}
......@@ -103,12 +113,26 @@
}
#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 *info = @"";
if (type == ExceptionTypeNet) {
info = @"当前网络环境较差,点击刷新重新加载~";
} else if (type == ExceptionTypeSmall) {
info = @"当前网络环境较差,点击刷新重新加载~";
} else if (type == ExceptionTypeNoData) {
info = @"空空如也哦~";
}
return info;
}
......@@ -119,6 +143,8 @@
title = @"刷新";
} else if (type == ExceptionTypeSmall) {
title = @"刷新";
} else if (type == ExceptionTypeNoData) {
title = @"重新获取";
}
return title;
}
......
......@@ -120,15 +120,21 @@
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
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 {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
self.exceptionView.hidden = NO;
self.aiWebView.hidden = YES;
DSLog(@"加载失败:%@", error);
DSLog(@"加载失败:%@", error.userInfo);
[self wkErrorDeal];
}
// 提交发生错误时调用
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
DSLog(@"提交失败:%@", error.userInfo);
[self wkErrorDeal];
}
// 内存不足出现白屏问题
......@@ -140,6 +146,30 @@
[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
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
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!