CNLiveWebViewController.m
10.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//
// CNLiveWebViewController.m
// CNLiveMineModule_Example
//
// Created by CNLive-zxw on 2019/12/7.
// Copyright © 2019 153993236@qq.com. All rights reserved.
//
#import "CNLiveWebViewController.h"
#import <WebKit/WebKit.h>
#import "CNLiveCategory.h"
@interface CNLiveWebViewController ()<WKUIDelegate, WKNavigationDelegate>
@property (nonatomic, copy) NSString *url;
@property (nonatomic, assign) BOOL isShow;
@property (nonatomic, strong) UIView *navigationBar;
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) UIProgressView *processView;
@property (nonatomic, weak) UILabel *titleLabel;
@end
@implementation CNLiveWebViewController
static const NSInteger margin = 10;
#pragma mark - 初始化方法
- (instancetype)initWithUrl:(NSString *)url title:(nullable NSString *)title{
return [self initWithUrl:url title:title isShow:NO];
}
- (instancetype)initWithUrl:(NSString *)url title:(nullable NSString *)title isShow:(BOOL)isShow {
self = [super init];
if (self) {
self.url = url;
self.title = title;
self.isShow = isShow;
}
return self;
}
- (void)setTitle:(NSString *)title{
_titleLabel.text = title;
}
#pragma mark - 加载数据
- (void)loadUrl {
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
[self.webView loadRequest:theRequest];
}
#pragma mark - 生命周期
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
if (@available(iOS 11, *)) {
self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
}
else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
[self createSubViews];
[self loadUrl];
}
- (void)dealloc {
[self.webView stopLoading];
[self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
if (!self.title) {
[self.webView removeObserver:self forKeyPath:@"title"];
}
self.webView.UIDelegate = nil;
self.webView.navigationDelegate = nil;
self.webView = nil;
}
- (void)createSubViews {
[self.view addSubview:self.navigationBar];
[self.view addSubview:self.webView];
[self.view addSubview:self.processView];
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
if (!self.title) {
[self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
}
}
#pragma mark - WKNavigationDelegate
// 在发送请求之前,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
//允许跳转
decisionHandler(WKNavigationActionPolicyAllow);
}
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
}
// 在收到响应后,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
//允许跳转
decisionHandler(WKNavigationResponsePolicyAllow);
//不允许跳转
//decisionHandler(WKNavigationResponsePolicyCancel);
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error{
NSLog(@"打印这个error%@",error);
}
// 接收到服务器跳转请求之后调用(重定向)
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
}
#pragma mark - 响应方法
//回退goBack
- (void)goBack {
if ([self.webView canGoBack]) {
[self.webView goBack];
}
else {
[self closeClick];
}
}
//关闭pop
- (void)closeClick {
if (self.presentingViewController != nil) {
[self dismissViewControllerAnimated:YES completion:nil];
}
else {
[self.navigationController popViewControllerAnimated:YES];
}
}
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"estimatedProgress"]) {
if (object == self.webView) {
[self.processView setAlpha:1.0f];
[self.processView setProgress:self.webView.estimatedProgress animated:YES];
if(self.webView.estimatedProgress >= 1.0f) {
[UIView animateWithDuration:0.3 delay:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.processView setAlpha:0.0f];
} completion:^(BOOL finished) {
[self.processView setProgress:0.0f animated:NO];
}];
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
else if ([keyPath isEqualToString:@"title"]){
if (object == self.webView) {
if (self.webView.title.length != 0) {
self.title = self.webView.title;
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark - 懒加载
- (UIView *)navigationBar{
if(!_navigationBar){
_navigationBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44+[UIApplication sharedApplication].statusBarFrame.size.height)];
_navigationBar.backgroundColor = [UIColor clearColor];
[self.view bringSubviewToFront:_navigationBar];
UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtn.adjustsImageWhenHighlighted = NO;
leftBtn.frame = CGRectMake(margin, [UIApplication sharedApplication].statusBarFrame.size.height, 44, 44);
UIImage *leftImage = [self getImageWithImageName:@"web_black_back" bundleName:@"CNLiveUserAgreementManager" targetClass:[self class]];
[leftBtn setImage:[leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[leftBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[_navigationBar addSubview:leftBtn];
UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
closeBtn.adjustsImageWhenHighlighted = NO;
closeBtn.frame = CGRectMake(44+margin, [UIApplication sharedApplication].statusBarFrame.size.height, 44, 44);
UIImage *closeImage = [self getImageWithImageName:@"web_black_close" bundleName:@"CNLiveUserAgreementManager" targetClass:[self class]];
[closeBtn setImage:[closeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
[closeBtn addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
[closeBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
closeBtn.hidden = !self.isShow;
[_navigationBar addSubview:closeBtn];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(([[UIScreen mainScreen] bounds].size.width - 200)/2.0, [UIApplication sharedApplication].statusBarFrame.size.height, 200, 44)];
titleLabel.font = [UIFont boldSystemFontOfSize:17];
titleLabel.textColor = [UIColor blackColor];
titleLabel.text = self.title;
titleLabel.textAlignment = NSTextAlignmentCenter;
[_navigationBar addSubview:titleLabel];
_titleLabel = titleLabel;
}
return _navigationBar;
}
- (WKWebView *)webView{
if(!_webView){
WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc] init];
configuration.allowsInlineMediaPlayback = YES;
WKPreferences *preferences = [WKPreferences new];
preferences.javaScriptCanOpenWindowsAutomatically = YES;
configuration.preferences = preferences;
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height+44, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-([UIApplication sharedApplication].statusBarFrame.size.height+44)) configuration:configuration];
_webView.UIDelegate = self;
_webView.navigationDelegate = self;
_webView.contentMode = UIViewContentModeScaleAspectFit;
_webView.scrollView.showsHorizontalScrollIndicator = NO;
_webView.scrollView.showsVerticalScrollIndicator = NO;
_webView.allowsBackForwardNavigationGestures = YES;
_webView.allowsLinkPreview = NO;
}
return _webView;
}
- (UIProgressView *)processView{
if(!_processView){
_processView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, ([UIApplication sharedApplication].statusBarFrame.size.height+44)+0.5, [[UIScreen mainScreen] bounds].size.width, 1)];
_processView.progressTintColor = [UIColor colorWithRed:19/255.0 green:204/255.0 blue:12/255.0 alpha:1.0];
_processView.trackTintColor = [UIColor whiteColor];
}
return _processView;
}
@end