Repository navigation
Expand file tree
/
Copy pathSHRMWebViewEngine.m
More file actions
executable file
·149 lines (117 loc) · 4.55 KB
/
Copy pathSHRMWebViewEngine.m
File metadata and controls
executable file
·149 lines (117 loc) · 4.55 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
//
// SHRMWebViewEngine.m
// Hybrid-framework
//
// Created by 王凯 on 2018/12/5.
// Copyright © 2018 王凯. All rights reserved.
//
#import "SHRMWebViewEngine.h"
#import "SHRMWebViewDelegate.h"
#import "SHRMUIWebViewJavaScriptBridge.h"
#import "SHRMCommandImpl.h"
@interface SHRMWebViewEngine ()
@property (nonatomic, strong) SHRMWebViewDelegate *WKWebViewDelegate;
@property (nonatomic, strong) SHRMWebPluginAnnotation *webPluginAnnotation;
@property (nonatomic, strong) NSMutableDictionary *pluginsMap;
@property (nonatomic, weak, readwrite) UIView *webView;
@end
@implementation SHRMWebViewEngine
@synthesize openWhiteList = _openWhiteList;
#pragma mark - public
+ (instancetype)bindBridgeWithWebView:(id)webView {
return [self bridge:webView];
}
- (void)setupPluginName:(NSString *)pluginName {
[self.pluginsMap setValue:pluginName forKey:[pluginName lowercaseString]];
}
- (id)getCommandInstance:(NSString*)pluginName {
NSString* className = [self.pluginsMap objectForKey:[pluginName lowercaseString]];
if (className == nil && self.isOpenWhiteList) {
NSLog(@"(pluginName: (%@) does not register on the whitelist.", pluginName);
return nil;
}
id obj = [[NSClassFromString(pluginName) alloc] initWithWebViewEngine:self];
if (obj != nil) {
[self registerPlugin:obj];
}else {
NSLog(@"(pluginName: (%@) does not exist.", pluginName);
}
return obj;
}
- (void)setWebViewDelegate:(NSObject<UIWebViewDelegate> *)webViewDelegate {
_webViewDelegate = webViewDelegate;
}
#pragma mark - privite
+ (instancetype)bridge:(id)webView {
SHRMWebViewEngine *bridge = [[SHRMWebViewEngine alloc] init];
bridge.webView = webView;
[bridge setupInstance];
[bridge loadStartupPlugin];
if ([webView isKindOfClass:[WKWebView class]]) {
[bridge configWKWebView:webView];
[bridge setJavaScriptBridge:bridge];
return bridge;
}
if ([webView isKindOfClass:[UIWebView class]]) {
SHRMUIWebViewJavaScriptBridge *uiBridge = [SHRMUIWebViewJavaScriptBridge bindBridgeWithWebView:webView];
[uiBridge setWebViewEngine:bridge];
[bridge setJavaScriptBridge:uiBridge];
return (SHRMWebViewEngine *)uiBridge;
}
[NSException raise:@"BadWebViewType" format:@"Unknown web view type."];
return nil;
}
- (void)configWKWebView:(WKWebView *)webView {
webView.navigationDelegate = _WKWebViewDelegate;
webView.configuration.userContentController = [[WKUserContentController alloc] init];
[webView.configuration.userContentController addScriptMessageHandler:self name:@"SHRMWKJSBridge"];
}
- (void)setupInstance{
_webViewhandleFactory = [[SHRMWebViewHandleFactory alloc] initWithWebViewEngine:self];
_WKWebViewDelegate = [[SHRMWebViewDelegate alloc] initWithWebViewEngine:self];
_webPluginAnnotation = [[SHRMWebPluginAnnotation alloc] initWithWebViewEngine:self];
_pluginsMap = [NSMutableDictionary dictionary];
_commandDelegate = [[SHRMCommandImpl alloc] initWithWebViewEngine:self];
self.openWhiteList = YES;
}
- (void)loadStartupPlugin {
[_webPluginAnnotation getAllRegisterPluginName];
}
- (void)registerPlugin:(SHRMBasePlugin *)plugin {
if ([plugin respondsToSelector:@selector(setCommandDelegate:)]) {
[plugin setCommandDelegate:_commandDelegate];
}
}
- (void)setJavaScriptBridge:(id<SHRMJavaScriptBridgeProtocol>)bridge {
_bridge = bridge;
}
#pragma mark - dealloc
- (void)dealloc {
if ([self.webView isKindOfClass:[WKWebView class]]) {
WKWebView *webView = (WKWebView *)self.webView;
[webView.configuration.userContentController removeScriptMessageHandlerForName:@"SHRMWKJSBridge"];
}
}
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([message.body isKindOfClass:[NSString class]]) {
[_webViewhandleFactory handleMsgCommand:message.body];
}
}
#pragma mark - SHRMJavaScriptBridgeProtocol
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(id , NSError *))completionHandler {
WKWebView *webView = (WKWebView *)self.webView;
[webView evaluateJavaScript:javaScriptString completionHandler:^(id obj, NSError * _Nullable error) {
if (completionHandler) {
completionHandler(obj, error);
}
}];
}
- (void)reload {
WKWebView *webView = (WKWebView *)self.webView;
[webView reload];
}
- (void)setOpenWhiteList:(BOOL)openWhiteList {
_openWhiteList = openWhiteList;
}
@end