-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWKWebViewController.m
More file actions
151 lines (129 loc) · 5.38 KB
/
Copy pathWKWebViewController.m
File metadata and controls
151 lines (129 loc) · 5.38 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
//
// WKWebViewController.m
// WebViewImageCache
//
// Created by txooo on 2018/12/28.
// Copyright © 2018 lingjye. All rights reserved.
//
#import "WKWebViewController.h"
#import <WebKit/WebKit.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "NSURLProtocol+WKWebview.h"
@interface WKWebViewController () <WKNavigationDelegate, WKUIDelegate>
@property (nonatomic, strong) WKWebView *wkWebView;
@property (nonatomic, strong) NSMutableArray *imageUrls;
@end
@implementation WKWebViewController
- (void)dealloc {
NSLog(@"%s", __func__);
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.edgesForExtendedLayout = UIRectEdgeAll;
if (@available(ios 11.0, *)) {
[[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentAutomatic];
}
self.view.backgroundColor = UIColor.whiteColor;
[NSURLProtocol wk_registerScheme:@"http"];
[NSURLProtocol wk_registerScheme:@"https"];
[self.view addSubview:self.wkWebView];
[self.wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"demo_1"
ofType:@"html"]]]];
}
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSString *absoluteString = navigationAction.request.URL.absoluteString;
if ([absoluteString hasPrefix:@"ljwebimageclick:"]) {
//获取点击图片index
NSInteger index = [[absoluteString substringFromIndex:@"ljWebImageClick:".length] integerValue];
if (index > self.imageUrls.count) {
index = 0;
}
[self showBigImageWithUrl:self.imageUrls[index]];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
- (void)showBigImageWithUrl:(NSString *)url {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imageView.backgroundColor = [UIColor blackColor];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.userInteractionEnabled = YES;
[imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil];
[[UIApplication sharedApplication].keyWindow addSubview:imageView];
imageView.alpha = 0;
[UIView animateWithDuration:0.3
animations:^{
imageView.alpha = 1;
}];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)];
[imageView addGestureRecognizer:tap];
}
- (void)tapImage:(UITapGestureRecognizer *)tap {
UIImageView *imageView = (UIImageView *)tap.view;
[UIView animateWithDuration:0.2
animations:^{
imageView.alpha = 0;
}
completion:^(BOOL finished) {
[imageView removeFromSuperview];
}];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
static NSString *const jsGetImages = @"function getImages(){\
var objs = document.getElementsByTagName(\"img\");\
var imgSrc = '';\
for(var i=0;i<objs.length;i++){\
imgSrc = imgSrc + objs[i].src + ';';\
+function( _i ){\
objs[ _i ].onclick = function(){\
document.location=\"ljWebImageClick:\" + _i;\
};\
} ( i );\
};\
return imgSrc;\
};";
[webView evaluateJavaScript:jsGetImages
completionHandler:^(id _Nullable obj, NSError *_Nullable error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
} else {
[webView evaluateJavaScript:@"getImages()"
completionHandler:^(id _Nullable result, NSError *_Nullable error) {
NSMutableArray *mutImgs =
[NSMutableArray arrayWithArray:[result componentsSeparatedByString:@";"]];
[mutImgs removeObject:@""];
[self.imageUrls addObjectsFromArray:mutImgs];
}];
}
}];
}
- (WKWebView *)wkWebView {
if (!_wkWebView) {
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = [[WKUserContentController alloc] init];
WKPreferences *preferences = [WKPreferences new];
preferences.javaScriptCanOpenWindowsAutomatically = YES;
preferences.minimumFontSize = 30.0;
configuration.preferences = preferences;
_wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
if ([_wkWebView respondsToSelector:@selector(setNavigationDelegate:)]) {
[_wkWebView setNavigationDelegate:self];
}
if ([_wkWebView respondsToSelector:@selector(setDelegate:)]) {
[_wkWebView setUIDelegate:self];
}
}
return _wkWebView;
}
- (NSMutableArray *)imageUrls {
if (!_imageUrls) {
_imageUrls = [NSMutableArray array];
}
return _imageUrls;
}
@end