@@ -14,60 +14,33 @@ @interface WKWebViewEngine ()
14
14
15
15
@property (nonatomic , strong ) WKWebViewDelegate *WKWebViewDelegate;
16
16
@property (nonatomic , strong ) WKWebPluginAnnotation *webPluginAnnotation;
17
+ @property (nonatomic , strong ) WKWebViewHandleFactory *webViewhandleFactory;
17
18
@property (nonatomic , strong ) NSMutableDictionary *pluginsMap;
18
19
@property (nonatomic , weak , readwrite ) WKWebView *webView;
20
+ @property (nonatomic , weak , readwrite ) NSObject <WKNavigationDelegate >*webViewDelegate;
19
21
20
22
@end
21
23
22
24
@implementation WKWebViewEngine
23
25
24
- @synthesize openWhiteList = _openWhiteList;
25
-
26
26
#pragma mark - public
27
27
28
- + (instancetype )bindBridgeWithWebView : (WKWebView *)webView {
29
- return [self bridge: webView];
30
- }
31
-
32
- - (void )setupPluginName : (NSString *)pluginName {
33
- [self .pluginsMap setValue: pluginName forKey: [pluginName lowercaseString ]];
34
- }
35
-
36
- - (id )getCommandInstance : (NSString *)pluginName {
37
- NSString * className = [self .pluginsMap objectForKey: [pluginName lowercaseString ]];
38
- if (className == nil && self.isOpenWhiteList ) {
39
- #ifdef DEBUG
40
- NSLog (@" (pluginName: (%@ ) does not register on the whitelist." , pluginName);
41
- #endif
42
- return nil ;
43
- }
44
-
45
- id obj = [[NSClassFromString (pluginName) alloc ] initWithWebViewEngine: self ];
46
- if (obj != nil ) {
47
- [self registerPlugin: obj];
48
- }else {
49
- #ifdef DEBUG
50
- NSLog (@" (pluginName: (%@ ) does not exist." , pluginName);
51
- #endif
52
- }
53
- return obj;
54
- }
55
-
56
- - (void )setWebViewDelegate : (NSObject <WKNavigationDelegate > *)webViewDelegate {
57
- _webViewDelegate = webViewDelegate;
28
+ + (instancetype )bindBridgeWithWebView : (WKWebView *)webView withDelegate : (NSObject <WKNavigationDelegate >*)webViewDelegate {
29
+ return [self bridge: webView delegate: webViewDelegate];
58
30
}
59
31
60
32
#pragma mark - privite
61
33
62
- + (instancetype )bridge : (WKWebView *)webView {
34
+ + (instancetype )bridge : (WKWebView *)webView delegate : ( NSObject < WKNavigationDelegate >*) webViewDelegate {
63
35
WKWebViewEngine *bridge = [[WKWebViewEngine alloc ] init ];
64
- bridge.webView = webView;
65
- [bridge setupInstance ];
66
- [bridge loadStartupPlugin ];
67
-
68
36
if ([webView isKindOfClass: [WKWebView class ]]) {
37
+ bridge.webViewDelegate = webViewDelegate;
38
+ bridge.webView = webView;
39
+
40
+ [bridge setupInstance ];
69
41
[bridge configWKWebView: webView];
70
42
[bridge setJavaScriptBridge: bridge];
43
+ [bridge loadStartupPlugin ];
71
44
return bridge;
72
45
}
73
46
@@ -78,7 +51,7 @@ + (instancetype)bridge:(WKWebView *)webView {
78
51
- (void )configWKWebView : (WKWebView *)webView {
79
52
webView.navigationDelegate = _WKWebViewDelegate;
80
53
webView.configuration .userContentController = [[WKUserContentController alloc ] init ];
81
- [webView.configuration.userContentController addScriptMessageHandler: self name: @" WKWKJSBridge " ];
54
+ [webView.configuration.userContentController addScriptMessageHandler: self name: @" WKJSBridge " ];
82
55
}
83
56
84
57
- (void )setupInstance {
@@ -95,7 +68,6 @@ - (void)loadStartupPlugin {
95
68
}
96
69
97
70
- (void )registerPlugin : (WKBasePlugin *)plugin {
98
-
99
71
if ([plugin respondsToSelector: @selector (setCommandDelegate: )]) {
100
72
[plugin setCommandDelegate: _commandDelegate];
101
73
}
@@ -105,15 +77,6 @@ - (void)setJavaScriptBridge:(id<WKJavaScriptBridgeProtocol>)bridge {
105
77
_bridge = bridge;
106
78
}
107
79
108
- #pragma mark - dealloc
109
-
110
- - (void )dealloc {
111
- if ([self .webView isKindOfClass: [WKWebView class ]]) {
112
- WKWebView *webView = (WKWebView *)self.webView ;
113
- [webView.configuration.userContentController removeScriptMessageHandlerForName: @" WKWKJSBridge" ];
114
- }
115
- }
116
-
117
80
#pragma mark - WKScriptMessageHandler
118
81
119
82
- (void )userContentController : (WKUserContentController *)userContentController didReceiveScriptMessage : (WKScriptMessage *)message {
@@ -132,13 +95,34 @@ - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void
132
95
}];
133
96
}
134
97
135
- - (void )reload {
136
- [self .webView reload ];
98
+ - (void )setupPluginName : ( NSString *) pluginName {
99
+ [self .pluginsMap setValue: pluginName forKey: [pluginName lowercaseString ] ];
137
100
}
138
101
139
- - (void )setOpenWhiteList : (BOOL )openWhiteList {
140
- _openWhiteList = openWhiteList;
102
+ - (id )getCommandInstance : (NSString *)pluginName {
103
+ NSString * className = [self .pluginsMap objectForKey: [pluginName lowercaseString ]];
104
+ if (className == nil && self.isOpenWhiteList ) {
105
+ #ifdef DEBUG
106
+ NSLog (@" (pluginName: (%@ ) does not register on the whitelist." , pluginName);
107
+ #endif
108
+ return nil ;
109
+ }
110
+
111
+ id obj = [[NSClassFromString (pluginName) alloc ] initWithWebViewEngine: self ];
112
+ if (obj != nil ) {
113
+ [self registerPlugin: obj];
114
+ }else {
115
+ #ifdef DEBUG
116
+ NSLog (@" (pluginName: (%@ ) does not exist." , pluginName);
117
+ #endif
118
+ }
119
+ return obj;
141
120
}
142
121
122
+ #pragma mark - dealloc
123
+
124
+ - (void )dealloc {
125
+ [self .webView.configuration.userContentController removeScriptMessageHandlerForName: @" WKJSBridge" ];
126
+ }
143
127
144
128
@end
0 commit comments