forked from pro648/BasicDemos-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
290 lines (235 loc) · 12.5 KB
/
Copy pathViewController.m
File metadata and controls
290 lines (235 loc) · 12.5 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
282
283
284
285
286
287
288
289
290
// ViewController.m
// WebKit
//
// Created by pro648 on 2018/12/24
// Copyright © 2018 pro648. All rights reserved.
//
#import "ViewController.h"
#import <WebKit/WebKit.h>
#import "ContentsViewController.h"
#import "URLSchemeHandler.h"
#import "UIDelegateHandler.h"
#import "NavigationDelegateHandler.h"
#import "ScriptMessageHandler.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIToolbar *toolBar;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *backButton;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *forwardButton;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *contentsButton;
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) WKWebViewConfiguration *webConfiguration;
@property (nonatomic, strong) UIBarButtonItem *refreshButton;
@property (nonatomic, strong) UIBarButtonItem *stopLoadingButton;
@property (nonatomic, strong) NSArray *messageBody;
@property (nonatomic, strong) NavigationDelegateHandler *navigationDelegateHandler;
@property (nonatomic, strong) UIDelegateHandler *uiDelegateHandler;
@property (nonatomic, strong) ScriptMessageHandler *scriptMessageHandler;
@property (nonatomic, strong) URLSchemeHandler *urlSchemeHandler;
@end
static void *webViewContext = &webViewContext;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"pro648";
self.backButton.enabled = NO;
self.forwardButton.enabled = NO;
[self.view insertSubview:self.webView belowSubview:self.progressView];
self.webView.translatesAutoresizingMaskIntoConstraints = NO;
UILayoutGuide *layoutGuide = self.view.safeAreaLayoutGuide;
[self.webView.topAnchor constraintEqualToAnchor:layoutGuide.topAnchor].active = YES;
[self.webView.leadingAnchor constraintEqualToAnchor:layoutGuide.leadingAnchor].active = YES;
[self.webView.bottomAnchor constraintEqualToAnchor:self.toolBar.topAnchor].active = YES;
[self.webView.trailingAnchor constraintEqualToAnchor:layoutGuide.trailingAnchor].active = YES;
[self.webView addObserver:self forKeyPath:@"hasOnlySecureContent" options:NSKeyValueObservingOptionNew context:webViewContext];
[self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:webViewContext];
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:webViewContext];
[self.webView addObserver:self forKeyPath:@"loading" options:NSKeyValueObservingOptionNew context:webViewContext];
[self.scriptMessageHandler addObserver:self forKeyPath:@"messageBody" options:NSKeyValueObservingOptionNew context:NULL];
// Local HTML
NSURL *htmlURL = [[NSBundle mainBundle] URLForResource:@"WKWebView - NSHipster" withExtension:@"htm"];
// Create WebView HTML
// NSURL *htmlURL = [[NSBundle mainBundle] URLForResource:@"web" withExtension:@"htm"];
// Custom url Scheme HTML
// NSURL *htmlURL = [[NSBundle mainBundle] URLForResource:@"customScheme" withExtension:@"htm"];
NSURL *baseURL = [htmlURL URLByDeletingLastPathComponent];
NSString *htmlString = [NSString stringWithContentsOfURL:htmlURL
encoding:NSUTF8StringEncoding
error:NULL];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
/*
// Web Content
// NSURL *myURL = [NSURL URLWithString:@"https://en.wikipedia.org/w/index.php?title=San_Francisco&mobileaction=toggle_view_desktop"];
NSURLRequest *request = [NSURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30];
[self.webView loadRequest:request];
*/
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable userAgent, NSError * _Nullable error) {
NSLog(@"%@",userAgent);
}];
}
#pragma mark - IBAction
- (IBAction)backButtonTapped:(id)sender {
[self.webView goBack];
}
- (IBAction)forwardButtonTapped:(id)sender {
[self.webView goForward];
}
- (void)refreshButtonTapped:(id)sender {
[self.webView reload];
}
- (void)stopLoadingButtonTapped:(id)sender {
[self.webView stopLoading];
}
- (IBAction)contentsButtonTapped:(id)sender {
ContentsViewController *contentsVC = [[ContentsViewController alloc] init];
contentsVC.messageBody = self.messageBody.copy;
[contentsVC setDidSelectEntry:^(Entry * _Nonnull entry) {
NSURLRequest *request = [NSURLRequest requestWithURL:entry.url];
[self.webView loadRequest:request];
}];
[self presentViewController:contentsVC
animated:YES
completion:nil];
}
- (IBAction)takeSnapShot:(UIBarButtonItem *)sender {
WKSnapshotConfiguration *shotConfiguration = [[WKSnapshotConfiguration alloc] init];
shotConfiguration.rect = CGRectMake(0, 0, self.webView.bounds.size.width, self.webView.bounds.size.height);
[self.webView takeSnapshotWithConfiguration:shotConfiguration
completionHandler:^(UIImage * _Nullable snapshotImage, NSError * _Nullable error) {
// 保存截图至相册,需要在info.plist中添加NSPhotoLibraryAddUsageDescription key和描述。
UIImageWriteToSavedPhotosAlbum(snapshotImage, NULL, NULL, NULL);
}];
}
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if (context == webViewContext && [keyPath isEqualToString:@"hasOnlySecureContent"]) {
BOOL onlySecureContent = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
NSLog(@"onlySecureContent:%@",onlySecureContent ? @"YES" : @"NO");
} else if (context == webViewContext && [keyPath isEqualToString:@"title"]) {
self.navigationItem.title = change[NSKeyValueChangeNewKey];
} else if (context == webViewContext && [keyPath isEqualToString:@"estimatedProgress"]) {
self.progressView.hidden = [change[NSKeyValueChangeNewKey] isEqualToNumber:@1];
CGFloat progress = [change[NSKeyValueChangeNewKey] floatValue];
self.progressView.progress = progress;
} else if (context == webViewContext && [keyPath isEqualToString:@"loading"]) {
BOOL loading = [change[NSKeyValueChangeNewKey] boolValue];
// 加载完成后,右侧为刷新按钮;加载过程中,右侧为暂停按钮。
self.navigationItem.rightBarButtonItem = loading ? self.stopLoadingButton : self.refreshButton;
self.backButton.enabled = self.webView.canGoBack;
self.forwardButton.enabled = self.webView.canGoForward;
} else if ([keyPath isEqualToString:@"messageBody"]) {
self.messageBody = (NSArray *)change[NSKeyValueChangeNewKey];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#pragma mark - Getters & Setters
- (NavigationDelegateHandler *)navigationDelegateHandler {
if (!_navigationDelegateHandler) {
_navigationDelegateHandler = [[NavigationDelegateHandler alloc] init];
_navigationDelegateHandler.backButton = self.backButton;
_navigationDelegateHandler.forwardButton = self.forwardButton;
}
return _navigationDelegateHandler;
}
- (UIDelegateHandler *)uiDelegateHandler {
if (!_uiDelegateHandler) {
_uiDelegateHandler = [[UIDelegateHandler alloc] init];
}
return _uiDelegateHandler;
}
- (ScriptMessageHandler *)scriptMessageHandler {
if (!_scriptMessageHandler) {
_scriptMessageHandler = [[ScriptMessageHandler alloc] init];
}
return _scriptMessageHandler;
}
- (URLSchemeHandler *)urlSchemeHandler {
if (!_urlSchemeHandler) {
_urlSchemeHandler = [[URLSchemeHandler alloc] init];
}
return _urlSchemeHandler;
}
- (WKWebView *)webView {
if (!_webView) {
_webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:self.webConfiguration];
_webView.allowsBackForwardNavigationGestures = YES;
// _webView.customUserAgent = @"pro648";
_webView.navigationDelegate = self.navigationDelegateHandler;
_webView.UIDelegate = self.uiDelegateHandler;
}
return _webView;
}
- (WKWebViewConfiguration *)webConfiguration {
if (!_webConfiguration) {
_webConfiguration = [[WKWebViewConfiguration alloc] init];
/*
// 偏好设置 设置最小字体
WKPreferences *preferences = [[WKPreferences alloc] init];
preferences.minimumFontSize = 30;
_webConfiguration.preferences = preferences;
*/
// 识别网页中的电话号码
_webConfiguration.dataDetectorTypes = WKDataDetectorTypePhoneNumber;
// Web视图内容完全加载到内存之前,禁止呈现。
_webConfiguration.suppressesIncrementalRendering = YES;
// _webConfiguration.applicationNameForUserAgent = @"pro648";
// 隐藏wikipedia左边缘和contents表格
NSURL *hideTableOfContentsScriptURL = [[NSBundle mainBundle] URLForResource:@"hide" withExtension:@"js"];
NSString *hideTableOfContentsScriptString = [NSString stringWithContentsOfURL:hideTableOfContentsScriptURL
encoding:NSUTF8StringEncoding error:NULL];
WKUserScript *hideTableOfContentsScript = [[WKUserScript alloc] initWithSource:hideTableOfContentsScriptString
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:YES];
// 获取contents表格内容
NSString *fetchTableOfContentsScriptString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"fetch" withExtension:@"js"] encoding:NSUTF8StringEncoding error:NULL];
WKUserScript *fetchTableOfContentsScript = [[WKUserScript alloc] initWithSource:fetchTableOfContentsScriptString
injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
forMainFrameOnly:YES];
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addUserScript:hideTableOfContentsScript];
[userContentController addUserScript:fetchTableOfContentsScript];
[userContentController addScriptMessageHandler:self.scriptMessageHandler name:@"didFetchTableOfContents"];
// 最后,将userContentController添加到WKWebViewConfiguration
_webConfiguration.userContentController = userContentController;
// 添加要自定义的url scheme
[_webConfiguration setURLSchemeHandler:self.urlSchemeHandler forURLScheme:@"custom-scheme"];
}
return _webConfiguration;
}
- (UIBarButtonItem *)refreshButton {
if (!_refreshButton) {
_refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:@selector(refreshButtonTapped:)];
}
return _refreshButton;
}
- (UIBarButtonItem *)stopLoadingButton {
if (!_stopLoadingButton) {
_stopLoadingButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:@selector(stopLoadingButtonTapped:)];
}
return _stopLoadingButton;
}
- (void)dealloc {
[_webView removeObserver:self
forKeyPath:@"hasOnlySecureContent"
context:webViewContext];
[_webView removeObserver:self
forKeyPath:@"title"
context:webViewContext];
[_webView removeObserver:self
forKeyPath:@"loading"
context:webViewContext];
[_webView removeObserver:self
forKeyPath:@"estimatedProgress"
context:webViewContext];
}
@end