forked from illusionspaces/WKJavaScriptBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHRMWebViewEngine.m
More file actions
executable file
·145 lines (115 loc) · 4.64 KB
/
Copy pathSHRMWebViewEngine.m
File metadata and controls
executable file
·145 lines (115 loc) · 4.64 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
//
// 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 *pluginObject;
@property (nonatomic, weak, readwrite) id webView;
@end
@implementation SHRMWebViewEngine
#pragma mark - public
+ (instancetype)bindBridgeWithWebView:(id)webView {
return [self bridge:webView];
}
- (void)setupPluginName:(NSString *)pluginName onload:(NSNumber *)onload {
if ([onload boolValue]) {
[self getCommandInstance:pluginName];
}
}
- (id)getCommandInstance:(NSString*)pluginName {
id obj = [_pluginObject objectForKey:[pluginName lowercaseString]];
if (!obj) {
obj = [[NSClassFromString(pluginName) alloc] initWithWebViewEngine:self];
if (!obj) {
NSString* fullClassName = [NSString stringWithFormat:@"%@.%@",
NSBundle.mainBundle.infoDictionary[@"CFBundleExecutable"],
pluginName];
obj = [[NSClassFromString(fullClassName)alloc] init];
}
if (obj != nil) {
[self registerPlugin:obj withPluginName:pluginName];
}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];
_pluginObject = [NSMutableDictionary dictionary];
_commandDelegate = [[SHRMCommandImpl alloc] initWithWebViewEngine:self];
}
- (void)loadStartupPlugin {
[_webPluginAnnotation getAllRegisterPluginName];
}
- (void)registerPlugin:(SHRMBasePlugin *)plugin withPluginName:(NSString *)pluginName {
if ([plugin respondsToSelector:@selector(setCommandDelegate:)]) {
[plugin setCommandDelegate:_commandDelegate];
}
[_pluginObject setObject:plugin forKey:[pluginName lowercaseString]];
}
- (void)setJavaScriptBridge:(id<SHRMJavaScriptBridgeProtocol>)bridge {
_bridge = bridge;
}
#pragma mark - dealloc
- (void)dealloc {
if ([self.webView isKindOfClass:[WKWebView class]]) {
WKWebView *webView = self.webView;
[webView.configuration.userContentController removeScriptMessageHandlerForName:@"SHRMWKJSBridge"];
}
}
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([message.body isKindOfClass:[NSArray class]]) {
[_webViewhandleFactory handleMsgCommand:message.body];
}
}
#pragma mark - SHRMJavaScriptBridgeProtocol
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(id , NSError *))completionHandler {
WKWebView *webView = self.webView;
[webView evaluateJavaScript:javaScriptString completionHandler:^(id obj, NSError * _Nullable error) {
if (completionHandler) {
completionHandler(obj, error);
}
}];
}
@end