-
Notifications
You must be signed in to change notification settings - Fork 0
/
DYFDisplayViewController.m
321 lines (252 loc) · 10.3 KB
/
DYFDisplayViewController.m
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// DYFDisplayViewController.m
//
// Created by dyf on 17/5/27.
// Copyright © 2017 dyf. All rights reserved.
//
#import "DYFDisplayViewController.h"
#import "DYFWebProgressView.h"
#define RGB_V(v) (v)/255.0
#define SCR_S UIScreen.mainScreen.bounds.size
#define SCR_W SCR_S.width
#define SCR_H SCR_S.height
@interface DYFDisplayViewController () <WKNavigationDelegate, WKUIDelegate>
@property (strong, nonatomic) UIToolbar *wk_toolBar;
@property (nonatomic, strong) DYFWebProgressView *progressView;
@end
@implementation DYFDisplayViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
NSLog(@"%s", __FUNCTION__);
}
return self;
}
- (void)loadView {
[super loadView];
[self addProgressView];
[self addWKWebView];
[self addWKToolBar];
}
- (void)viewDidLoad {
[super viewDidLoad];
UIColor *bgColor = [UIColor colorWithRed:RGB_V(243)
green:RGB_V(243)
blue:RGB_V(243)
alpha:1];
self.view.backgroundColor = bgColor;
[self loadRequest];
}
- (BOOL)hasSafeArea {
BOOL hasSafeArea = NO;
UIApplication *sharedApp = UIApplication.sharedApplication;
if (@available(iOS 11.0, *)) {
hasSafeArea = sharedApp.delegate.window.safeAreaInsets.bottom > 0;
}
return hasSafeArea;
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
CGFloat topM = self.hasSafeArea ? (44.f + 44.f) : (20.f + 44.f);
CGFloat bottomM = self.hasSafeArea ? 34.f : 0.f;
CGFloat tbH = 44.f;
CGRect tbFrame;
tbFrame.origin.x = 0;
tbFrame.origin.y = CGRectGetHeight(self.view.bounds) - tbH;
tbFrame.size.width = CGRectGetWidth(self.view.bounds);
tbFrame.size.height = tbH;
self.wk_toolBar.frame = tbFrame;
[self adjustWKToolBar];
CGFloat x = 0;
CGFloat y = 0;
CGFloat w = tbFrame.size.width;
CGFloat h = tbFrame.origin.y;
self.wk_webView.frame = CGRectMake(x, y, w, h);
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.progressView endLoading];
}
- (WKWebViewConfiguration *)wk_webViewConfiguration {
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
WKPreferences *preferences = [[WKPreferences alloc] init];
preferences.minimumFontSize = 0;
preferences.javaScriptEnabled = YES;
preferences.javaScriptCanOpenWindowsAutomatically = YES;
config.preferences = preferences;
config.allowsInlineMediaPlayback = YES;
if (@available(iOS 9.0, *))
{
config.allowsPictureInPictureMediaPlayback = YES;
config.allowsAirPlayForMediaPlayback = YES;
if (@available(iOS 10.0, *)) {
config.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeAll;
} else {
config.requiresUserActionForMediaPlayback = YES;
}
}
else {
// Fallback on earlier versions
config.mediaPlaybackAllowsAirPlay = YES;
config.mediaPlaybackRequiresUserAction = YES;
}
return config;
}
- (WKWebView *)wk_webView {
if (!_wk_webView) {
_wk_webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:self.wk_webViewConfiguration];
}
return _wk_webView;
}
- (void)addWKWebView {
UIColor *bgColor = [UIColor colorWithRed:RGB_V(243)
green:RGB_V(243)
blue:RGB_V(243)
alpha:1];
self.wk_webView.backgroundColor = bgColor;
self.wk_webView.opaque = NO;
self.wk_webView.navigationDelegate = self;
self.wk_webView.UIDelegate = self;
self.wk_webView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
[self.view addSubview:self.wk_webView];
}
- (void)addWKToolBar {
self.wk_toolBar.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
[self.view addSubview:self.wk_toolBar];
}
- (UIToolbar *)wk_toolBar {
if (!_wk_toolBar) {
_wk_toolBar = [[UIToolbar alloc] init];
_wk_toolBar.barStyle = UIBarStyleDefault;
}
return _wk_toolBar;
}
- (void)adjustWKToolBar {
if (_wk_toolBar) {
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *rewindItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(onBack:)];
UIBarButtonItem *forWardItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(onForward:)];
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(onRefresh:)];
UIBarButtonItem *stopItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(onStop:)];
NSArray *items = @[flexibleSpace, rewindItem,
flexibleSpace, forWardItem,
flexibleSpace, refreshItem,
flexibleSpace, stopItem,
flexibleSpace];
[self.wk_toolBar setItems:items animated:YES];
}
}
- (void)loadRequest {
NSURL *aURL = [NSURL URLWithString:self.aUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:aURL];
[self.wk_webView loadRequest:request];
}
- (void)onBack:(id)sender {
if ([self.wk_webView canGoBack]) {
[self.wk_webView goBack];
}
}
- (void)onForward:(id)sender {
if ([self.wk_webView canGoForward]) {
[self.wk_webView goForward];
}
}
- (void)onRefresh:(id)sender {
[self.wk_webView reload];
}
- (void)onStop:(id)sender {
if ([self.wk_webView isLoading]) {
[self.wk_webView stopLoading];
}
}
- (void)addProgressView {
if (!_progressView) {
[self.navigationBar addSubview:self.progressView];
}
}
- (void)setupNavigationItemTitle {
self.navigationItem.title = self.wk_webView.title;
}
#pragma make - WKNavigationDelegate, WKUIDelegate
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
// didStartProvisionalNavigation.
NSURL *aURL = [webView.URL copy];
NSLog(@"%s url: %@", __FUNCTION__, aURL);
[self.progressView startLoading];
}
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
// didReceiveServerRedirectForProvisionalNavigation.
NSLog(@"%s url: %@", __FUNCTION__, webView.URL);
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
NSLog(@"%s", __FUNCTION__);
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
NSLog(@"%s", __FUNCTION__);
[self.progressView endLoading];
[self setupNavigationItemTitle];
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
if (!error) { return; }
NSString *errMessage = [NSString stringWithFormat:@"%zi, %@", error.code, error.localizedDescription];
NSLog(@"%s [error]: %@", __FUNCTION__, errMessage);
[self.progressView endLoading];
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
if (!error) { return; }
NSString *errMessage = [NSString stringWithFormat:@"%zi, %@", error.code, error.localizedDescription];
NSLog(@"%s [error]: %@", __FUNCTION__, errMessage);
[self.progressView endLoading];
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
// decidePolicyForNavigationAction.
[self setupNavigationItemTitle];
NSURL *aURL = [navigationAction.request.URL copy];
NSString *aUrl = aURL.absoluteString;
NSLog(@"%s url: %@", __FUNCTION__, aUrl);
if (![aUrl isEqualToString:@"about:blank"]) {}
// Method NO.1: resolve the problem about '_blank'.
//if (navigationAction.targetFrame == nil) {
//NSLog(@"- [webView loadRequest:navigationAction.request]");
//[webView loadRequest:navigationAction.request];
//}
decisionHandler(WKNavigationActionPolicyAllow);
}
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
// createWebViewWithConfiguration.
NSURL *aURL = [navigationAction.request.URL copy];
NSString *aUrl = aURL.absoluteString;
NSLog(@"%s url: %@", __FUNCTION__, aUrl);
if (!navigationAction.targetFrame.isMainFrame) {
NSLog(@"- [webView loadRequest:navigationAction.request]");
[webView loadRequest:navigationAction.request];
}
return nil;
}
- (DYFWebProgressView *)progressView {
if (!_progressView) {
CGFloat w = self.navigationBar.bounds.size.width;
CGFloat h = 3.f;
CGFloat x = 0.f;
CGFloat y = self.navigationBar.bounds.size.height - h;
_progressView = [[DYFWebProgressView alloc] initWithFrame:CGRectMake(x, y, w, h)];
_progressView.lineWidth = 3.f;
_progressView.lineColor = [UIColor colorWithRed:RGB_V(10) green:RGB_V(115) blue:RGB_V(255) alpha:1];
}
return _progressView;
}
- (UINavigationBar *)navigationBar {
return self.navigationController.navigationBar;
}
- (void)dealloc {
#ifdef DEBUG
NSLog(@"%s", __FUNCTION__);
#endif
if (_wk_webView) { _wk_webView = nil; }
}
@end