-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWKJSProxyDelegate.m
309 lines (271 loc) · 10.4 KB
/
WKJSProxyDelegate.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
//
// WKJSProxyDelegate.m
// zhs
//
// Created by zhs on 2017/8/17.
// Copyright © 2017年 zhs. All rights reserved.
//
#import "WKJSProxyDelegate.h"
#import "WKJSDataFunction.h"
#import <objc/runtime.h>
NSString* INJECT_JS = @"window.EasyJS = {\
__callbacks: {},\
\
invokeCallback: function (cbID, removeAfterExecute){\
var args = Array.prototype.slice.call(arguments);\
args.shift();\
args.shift();\
\
for (var i = 0, l = args.length; i < l; i++){\
args[i] = decodeURIComponent(args[i]);\
}\
\
var cb = EasyJS.__callbacks[cbID];\
if (removeAfterExecute){\
EasyJS.__callbacks[cbID] = undefined;\
}\
return cb.apply(null, args);\
},\
\
call: function (obj, functionName, args){\
var formattedArgs = [];\
for (var i = 0, l = args.length; i < l; i++){\
if (typeof args[i] == \"function\"){\
formattedArgs.push(\"f\");\
var cbID = \"__cb\" + (+new Date);\
EasyJS.__callbacks[cbID] = args[i];\
formattedArgs.push(cbID);\
}else{\
formattedArgs.push(\"s\");\
formattedArgs.push(encodeURIComponent(args[i]));\
}\
}\
\
var argStr = (formattedArgs.length > 0 ? \":\" + encodeURIComponent(formattedArgs.join(\":\")) : \"\");\
\
var iframe = document.createElement(\"IFRAME\");\
iframe.setAttribute(\"src\", \"easy-js:\" + obj + \":\" + encodeURIComponent(functionName) + argStr);\
document.documentElement.appendChild(iframe);\
iframe.parentNode.removeChild(iframe);\
iframe = null;\
\
var ret = EasyJS.retValue;\
EasyJS.retValue = undefined;\
\
if (ret){\
return decodeURIComponent(ret);\
}\
},\
\
inject: function (obj, methods){\
window[obj] = {};\
var jsObj = window[obj];\
\
for (var i = 0, l = methods.length; i < l; i++){\
(function (){\
var method = methods[i];\
var jsMethod = method.replace(new RegExp(\":\", \"g\"), \"\");\
jsObj[jsMethod] = function (){\
return EasyJS.call(obj, method, Array.prototype.slice.call(arguments));\
};\
})();\
}\
}\
};";
@implementation WKJSProxyDelegate
- (void)addJavascriptInterfaces:(NSObject*) interface WithName:(NSString*) name{
if (! self.javascriptInterfaces){
self.javascriptInterfaces = [[NSMutableDictionary alloc] init];
}
[self.javascriptInterfaces setValue:interface forKey:name];
}
#pragma WKNavigationDelegate
/**
* 在发送请求之前,决定是否跳转
*
* @param webView 实现该代理的webview
* @param navigationAction 当前navigation
* @param decisionHandler 是否调转block
*/
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURLRequest* request = navigationAction.request;
NSString *requestString = [[request URL] absoluteString];
ZHSWKWebView *zhsView = (ZHSWKWebView *)webView;
if ([requestString hasPrefix:@"easy-js:"]) {
[self handleEasyJS:zhsView requestString:requestString];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
if (!_wkWebViewDelegate){
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
[_wkWebViewDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
}
/**
* 页面开始加载时调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
[self injectJSScript:(ZHSWKWebView *)webView];
[_wkWebViewDelegate webView:webView didStartProvisionalNavigation:navigation];
}
/**
* 接收到服务器跳转请求之后调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
[_wkWebViewDelegate webView:webView didReceiveServerRedirectForProvisionalNavigation:navigation];
}
/**
* 在收到响应后,决定是否跳转
*
* @param webView 实现该代理的webview
* @param navigationResponse 当前navigation
* @param decisionHandler 是否跳转block
*/
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
decisionHandler(WKNavigationResponsePolicyAllow);
}
/**
* 当内容开始返回时调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
[_wkWebViewDelegate webView:webView didCommitNavigation:navigation];
}
/**
* 页面加载完成之后调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[self injectJSScript:(ZHSWKWebView *)webView];
[_wkWebViewDelegate webView:webView didFinishNavigation:navigation];
}
/**
* 加载失败时调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
* @param error 错误
*/
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
[_wkWebViewDelegate webView:webView didFailProvisionalNavigation:navigation withError:error];
}
/**
访问HTTPS链接
*/
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
if ([challenge previousFailureCount] == 0) {
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
} else {
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
}
} else {
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
}
}
#pragma 其他处理JS方法
//处理EasyJS方法
- (void)handleEasyJS:(ZHSWKWebView *)webView requestString:(NSString *)requestString{
/*
A sample URL structure:
easy-js:MyJSTest:test
easy-js:MyJSTest:testWithParam%3A:haha
*/
NSArray *components = [requestString componentsSeparatedByString:@":"];
//Easy_DLog(@"req: %@", requestString);
NSString* obj = (NSString*)[components objectAtIndex:1];
NSString* method = [(NSString*)[components objectAtIndex:2]
stringByRemovingPercentEncoding];
NSObject* interface = [_javascriptInterfaces objectForKey:obj];
// execute the interfacing method
SEL selector = NSSelectorFromString(method);
NSMethodSignature* sig = [[interface class] instanceMethodSignatureForSelector:selector];
NSInvocation* invoker = [NSInvocation invocationWithMethodSignature:sig];
invoker.selector = selector;
invoker.target = interface;
NSMutableArray* args = [[NSMutableArray alloc] init];
if ([components count] > 3){
NSString *argsAsString = [(NSString*)[components objectAtIndex:3]
stringByRemovingPercentEncoding];
NSArray* formattedArgs = [argsAsString componentsSeparatedByString:@":"];
for (int i = 0, j = 0, l = (int)[formattedArgs count]; i < l; i+=2, j++){
NSString* type = ((NSString*) [formattedArgs objectAtIndex:i]);
NSString* argStr = ((NSString*) [formattedArgs objectAtIndex:i + 1]);
if ([@"f" isEqualToString:type]){
WKJSDataFunction* func = [[WKJSDataFunction alloc] initWithWebView:webView];
func.funcID = argStr;
[args addObject:func];
[invoker setArgument:&func atIndex:(j + 2)];
}else if ([@"s" isEqualToString:type]){
NSString* arg = [argStr stringByRemovingPercentEncoding];
[args addObject:arg];
[invoker setArgument:&arg atIndex:(j + 2)];
}
}
}
[invoker invoke];
//return the value by using javascript
if ([sig methodReturnLength] > 0){
NSString * __unsafe_unretained tempRetValue;
[invoker getReturnValue:&tempRetValue];
NSString *retValue = tempRetValue;
if (retValue == NULL || retValue == nil){
[self evaluateJavaScript:@"EasyJS.retValue=null;" webView:webView];
}else{
retValue = [retValue stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"!*'();:@&=+$,/?%#[]"].invertedSet];
[self evaluateJavaScript:[@"" stringByAppendingFormat:@"EasyJS.retValue=\"%@\";", retValue] webView:webView];
}
}
}
//向JS中注入交互方法
- (void)injectJSScript:(ZHSWKWebView *)webView{
if (! self.javascriptInterfaces){
self.javascriptInterfaces = [[NSMutableDictionary alloc] init];
}
NSMutableString* injection = [[NSMutableString alloc] init];
//inject the javascript interface
for(id key in self.javascriptInterfaces) {
NSObject* interface = [self.javascriptInterfaces objectForKey:key];
[injection appendString:@"EasyJS.inject(\""];
[injection appendString:key];
[injection appendString:@"\", ["];
unsigned int mc = 0;
Class cls = object_getClass(interface);
Method * mlist = class_copyMethodList(cls, &mc);
for (int i = 0; i < mc; i++){
[injection appendString:@"\""];
[injection appendString:[NSString stringWithUTF8String:sel_getName(method_getName(mlist[i]))]];
[injection appendString:@"\""];
if (i != mc - 1){
[injection appendString:@", "];
}
}
free(mlist);
[injection appendString:@"]);"];
}
NSString* js = INJECT_JS;
//inject the basic functions first
[self evaluateJavaScript:js webView:webView];
//inject the function interface
[self evaluateJavaScript:injection webView:webView];
}
- (void)evaluateJavaScript:(NSString *)js webView:(ZHSWKWebView *)webView{
[webView evaluateJavaScript:js completionHandler:nil];
}
- (void)dealloc{
_javascriptInterfaces = nil;
_wkWebViewDelegate = nil;
}
@end