forked from dukeland/EasyJSWebView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEasyJSWebViewProxyDelegate.m
110 lines (93 loc) · 4.27 KB
/
EasyJSWebViewProxyDelegate.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
//
// EasyJSWebViewDelegate.m
// EasyJS
//
// Created by Lau Alex on 19/1/13.
// Modified by 腹黒い茶 on 2/3/2015.
// Copyright (c) 2013 Dukeland. All rights reserved.
//
#import "EasyJSWebViewProxyDelegate.h"
#import <objc/runtime.h>
@interface EasyJSWebViewProxyDelegate()
@property (nonatomic, readwrite) NSUInteger totalResources;
@end
@implementation EasyJSWebViewProxyDelegate
@synthesize javascriptInterfaces;
@synthesize INJECT_JS;
@synthesize webView;
- (instancetype)initWithWebView:(id)_webView {
self = [super init];
if (self) {
self.webView = _webView;
self.totalResources = 0;
NSString *injectFile = [[NSBundle mainBundle] pathForResource:@"easyjs-inject"
ofType:@"js"];
NSError *error = nil;
self.INJECT_JS = [[NSString alloc] initWithContentsOfFile:injectFile
encoding:NSUTF8StringEncoding
error:&error];
}
return self;
}
- (void)addJavascriptInterfaces:(NSObject *)interface WithName:(NSString *)name{
if (!self.javascriptInterfaces) {
self.javascriptInterfaces = [[NSMutableDictionary alloc] init];
}
[self.javascriptInterfaces setValue:interface forKey:name];
}
- (void)webView:(WKWebView *)_webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(WKWebView *)_webView didFinishNavigation:(WKNavigation *)navigation {
if (!self.javascriptInterfaces) {
self.javascriptInterfaces = [[NSMutableDictionary alloc] init];
}
NSMutableString *injection = [[NSMutableString alloc] init];
NSMutableString *initialize = [[NSMutableString alloc] init];
//inject the javascript interface
for(id key in self.javascriptInterfaces) {
NSObject *interface = [self.javascriptInterfaces objectForKey:key];
[initialize appendFormat:@"if (%@.initialize != undefined) { %@.initialize(); }", key, key];
[injection appendFormat:@"EasyJS.inject(\"%@\", [", key];
unsigned int mc = 0;
Class cls = object_getClass(interface);
Method *mlist = class_copyMethodList(cls, &mc);
for (int i = 0; i < mc; i++){
[injection appendFormat:@"\"%@\"", [NSString stringWithUTF8String:sel_getName(method_getName(mlist[i]))]];
if (i != mc - 1){
[injection appendString:@", "];
}
}
mlist = nil;
cls = nil;
[injection appendString:@"]);"];
}
// inject the basic functions first
[_webView evaluateJavaScript:self.INJECT_JS completionHandler:nil];
// inject the function interface
[_webView evaluateJavaScript:injection completionHandler:nil];
// initialize injected code
[_webView evaluateJavaScript:initialize completionHandler:nil];
self.totalResources++;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(WKWebView *)_webView didCommitNavigation:(WKNavigation *)navigation {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webView:(WKWebView *)_webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
decisionHandler(WKNavigationActionPolicyAllow);
}
- (void)webView:(WKWebView *)_webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler {
if (challenge.previousFailureCount == 0) {
NSURLCredentialPersistence persistence = NSURLCredentialPersistenceForSession;
NSURLCredential *credential = [NSURLCredential credentialWithUser:[_webView username]
password:[_webView password]
persistence:persistence];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
//NSLog(@"%@ > %@:%@", [_webView URL], [_webView username], [_webView password]);
} else {
NSLog(@"%s: challenge.error = %@", __FUNCTION__, challenge.error);
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
}
}
@end