DsWebController.m
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// DsWebController.m
// DreamSleep
//
// Created by peter on 2022/4/24.
//
#import "DsWebController.h"
#import <WebKit/webKit.h>
#import "DsMaskView.h"
@interface DsWebController () <WKNavigationDelegate>
@property (nonatomic, copy) NSString *naviTitle;
@property (nonatomic, strong) NSURL *linkUrl;
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) NSMutableURLRequest *request;
@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, strong) ExceptionDefaultView *exceptionView;
@end
@implementation DsWebController
- (instancetype)initWithTitle:(NSString *)title link:(NSString *)link {
if (self = [super init]) {
self.naviTitle = title;
self.linkUrl = [NSURL URLWithString:link];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = self.naviTitle;
self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
[self.view addSubview:self.webView];
[self.view addSubview:self.progressView];
[self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0));
}];
[self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self.view);
}];
}
- (void)dealloc {
[self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}
#pragma mark - WKWebView的监听方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"estimatedProgress"]) {
[self.progressView setProgress:self.webView.estimatedProgress animated:YES];
self.progressView.hidden = self.progressView.progress == 1;
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
self.exceptionView.hidden = YES;
[DsMaskView showMaskWithSuperView:self.view];
self.webView.hidden = NO;
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
DSLog(@"加载失败:%@", error.userInfo);
self.exceptionView.hidden = NO;
}
#pragma mark - lazy
- (WKWebView *)webView {
if (!_webView) {
_webView = [[WKWebView alloc] init];
_webView.navigationDelegate = self;
_webView.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
_webView.scrollView.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
_webView.scrollView.showsVerticalScrollIndicator = NO;
_webView.hidden = YES;
_webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
[_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:self.linkUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
self.request = request;
[_webView loadRequest:self.request];
}
return _webView;
}
- (UIProgressView *)progressView {
if (!_progressView) {
_progressView = [[UIProgressView alloc] init];
_progressView.progressTintColor = HighlightColor;
_progressView.dk_trackTintColorPicker = DKColorPickerWithKey(VCViewBG);
}
return _progressView;
}
- (ExceptionDefaultView *)exceptionView {
if (!_exceptionView) {
WS(weakSelf);
_exceptionView = [[ExceptionDefaultView alloc] initWithType:ExceptionTypeNet block:^{
weakSelf.progressView.hidden = NO;
weakSelf.progressView.progress = 0;
weakSelf.exceptionView.hidden = YES;
[weakSelf.webView loadRequest:self.request];
} superView:self.view];
}
return _exceptionView;
}
#pragma mark - H5页面导航栏为默认模式
- (NaviStyle)navigationBarStyle {
return NaviStyleDefault;
}
@end