Skip to content

Commit ecade60

Browse files
更新pod
1 parent 7b47a6e commit ecade60

File tree

8 files changed

+66
-95
lines changed

8 files changed

+66
-95
lines changed

.DS_Store

0 Bytes
Binary file not shown.

WKJavaScriptBridge/WKBasePlugin.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ - (instancetype)initWithWebViewEngine:(WKWebViewEngine *)webViewEngine {
2626

2727
- (UIViewController *)rootViewController {
2828
if (_webViewEngine != nil) {
29-
return self.webViewEngine.webViewDelegate;
29+
if ([self.webViewEngine.webViewDelegate isKindOfClass:[UIViewController class]]) {
30+
return (UIViewController *)self.webViewEngine.webViewDelegate;
31+
}else {
32+
return nil;
33+
}
3034
}
3135
return nil;
3236
}

WKJavaScriptBridge/WKCommandImpl.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,18 @@ - (void)evalJs:(NSString *)js {
7575
}
7676

7777
- (id)getCommandInstance:(NSString*)pluginName {
78-
return [_webViewEngine getCommandInstance:pluginName];
78+
return [_webViewEngine.bridge getCommandInstance:pluginName];
7979
}
8080

8181
- (void)runInBackground:(void (^)(void))block {
8282
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
8383
}
8484

8585
- (void)reloadWebView {
86-
[_webViewEngine.bridge reload];
86+
[_webViewEngine.webView reload];
8787
}
8888

89-
- (BOOL)isValidCallbackId:(NSString*)callbackId
90-
{
89+
- (BOOL)isValidCallbackId:(NSString*)callbackId {
9190
if ((callbackId == nil) || (_callbackIdPattern == nil)) {
9291
return NO;
9392
}

WKJavaScriptBridge/WKJavaScriptBridgeProtocol.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,27 @@ NS_ASSUME_NONNULL_BEGIN
1313
@protocol WKJavaScriptBridgeProtocol <NSObject>
1414

1515
/**
16-
native 调用 js接口
16+
执行JS
1717
1818
@param javaScriptString js
1919
@param completionHandler 结果回调
2020
*/
2121
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(id, NSError *))completionHandler;
2222

23-
2423
/**
25-
刷新webView
24+
插件加载
25+
26+
@param pluginName name
2627
*/
27-
- (void)reload;
28+
- (void)setupPluginName:(NSString *)pluginName;
2829

2930
/**
30-
是否开启插件白名单,默认开启
31+
获取plugin实例
32+
33+
@param pluginName plugin name
34+
@return instance
3135
*/
32-
@property (nonatomic, assign, getter=isOpenWhiteList) BOOL openWhiteList;
36+
- (id)getCommandInstance:(NSString*)pluginName;
3337

3438
@end
3539

WKJavaScriptBridge/WKWebPluginAnnotation.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ - (void)registedAnnotationModules {
3535
NSArray<NSString *>*services = [WKWebPluginAnnotation AnnotationModules];
3636
for (NSString *pluginName in services) {
3737
if (pluginName) {
38-
[_webViewEngine setupPluginName:pluginName];
38+
[_webViewEngine.bridge setupPluginName:pluginName];
3939
}
4040
}
4141
}

WKJavaScriptBridge/WKWebViewEngine.h

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,28 @@ NS_ASSUME_NONNULL_BEGIN
1919

2020
@class WKWebViewHandleFactory;
2121

22-
@interface WKWebViewEngine : NSObject <WKScriptMessageHandler, WKJavaScriptBridgeProtocol>
22+
@interface WKWebViewEngine : NSObject
2323

2424
@property (nonatomic, readonly, strong) id <WKCommandProtocol> commandDelegate;
25-
@property (nonatomic, readonly, strong) WKWebViewHandleFactory *webViewhandleFactory;
2625
@property (nonatomic, readonly, strong) id <WKJavaScriptBridgeProtocol>bridge;
2726

28-
/**
29-
是否开启插件白名单,默认开启
30-
*/
27+
/** 是否开启插件白名单,默认开启 */
3128
@property (nonatomic, assign, getter=isOpenWhiteList) BOOL openWhiteList;
32-
/**
33-
外部提供的webView
34-
*/
35-
@property (nonatomic, readonly, weak) WKWebView *webView;
3629

37-
/**
38-
webView容器VC
39-
*/
40-
@property (nonatomic, weak) id webViewDelegate;
30+
/** 外部提供的webView */
31+
@property (nonatomic, readonly, weak) WKWebView *webView;
4132

42-
/**
43-
webView绑定 bridge初始化
44-
45-
@param webView WKWebView
46-
@return bridge
47-
*/
48-
+ (instancetype)bindBridgeWithWebView:(WKWebView *)webView;
33+
/** webView的容器 */
34+
@property (nonatomic, readonly, weak) NSObject <WKNavigationDelegate>*webViewDelegate;
4935

5036
/**
51-
插件预加载接口 onload 为 1 表示提前初始化此插件对象 否则在调用时初始化
52-
53-
@param pluginName name
54-
*/
55-
- (void)setupPluginName:(NSString *)pluginName;
37+
初始化方法
5638
57-
/**
58-
获取plugin实例
59-
60-
@param pluginName plugin name
61-
@return instance
39+
@param webView webView
40+
@param webViewDelegate 实现webView代理的类,没有可传空
41+
@return bridge
6242
*/
63-
- (id)getCommandInstance:(NSString*)pluginName;
43+
+ (instancetype)bindBridgeWithWebView:(WKWebView *)webView withDelegate:(NSObject <WKNavigationDelegate>*)webViewDelegate;
6444

6545
@end
6646

WKJavaScriptBridge/WKWebViewEngine.m

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,33 @@ @interface WKWebViewEngine ()
1414

1515
@property (nonatomic, strong) WKWebViewDelegate *WKWebViewDelegate;
1616
@property (nonatomic, strong) WKWebPluginAnnotation *webPluginAnnotation;
17+
@property (nonatomic, strong) WKWebViewHandleFactory *webViewhandleFactory;
1718
@property (nonatomic, strong) NSMutableDictionary *pluginsMap;
1819
@property (nonatomic, weak, readwrite) WKWebView *webView;
20+
@property (nonatomic, weak, readwrite) NSObject <WKNavigationDelegate>*webViewDelegate;
1921

2022
@end
2123

2224
@implementation WKWebViewEngine
2325

24-
@synthesize openWhiteList = _openWhiteList;
25-
2626
#pragma mark - public
2727

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];
5830
}
5931

6032
#pragma mark - privite
6133

62-
+ (instancetype)bridge:(WKWebView *)webView {
34+
+ (instancetype)bridge:(WKWebView *)webView delegate:(NSObject <WKNavigationDelegate>*)webViewDelegate {
6335
WKWebViewEngine *bridge = [[WKWebViewEngine alloc] init];
64-
bridge.webView = webView;
65-
[bridge setupInstance];
66-
[bridge loadStartupPlugin];
67-
6836
if ([webView isKindOfClass:[WKWebView class]]) {
37+
bridge.webViewDelegate = webViewDelegate;
38+
bridge.webView = webView;
39+
40+
[bridge setupInstance];
6941
[bridge configWKWebView:webView];
7042
[bridge setJavaScriptBridge:bridge];
43+
[bridge loadStartupPlugin];
7144
return bridge;
7245
}
7346

@@ -78,7 +51,7 @@ + (instancetype)bridge:(WKWebView *)webView {
7851
- (void)configWKWebView:(WKWebView *)webView {
7952
webView.navigationDelegate = _WKWebViewDelegate;
8053
webView.configuration.userContentController = [[WKUserContentController alloc] init];
81-
[webView.configuration.userContentController addScriptMessageHandler:self name:@"WKWKJSBridge"];
54+
[webView.configuration.userContentController addScriptMessageHandler:self name:@"WKJSBridge"];
8255
}
8356

8457
- (void)setupInstance{
@@ -95,7 +68,6 @@ - (void)loadStartupPlugin {
9568
}
9669

9770
- (void)registerPlugin:(WKBasePlugin *)plugin {
98-
9971
if ([plugin respondsToSelector:@selector(setCommandDelegate:)]) {
10072
[plugin setCommandDelegate:_commandDelegate];
10173
}
@@ -105,15 +77,6 @@ - (void)setJavaScriptBridge:(id<WKJavaScriptBridgeProtocol>)bridge {
10577
_bridge = bridge;
10678
}
10779

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-
11780
#pragma mark - WKScriptMessageHandler
11881

11982
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
@@ -132,13 +95,34 @@ - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void
13295
}];
13396
}
13497

135-
- (void)reload {
136-
[self.webView reload];
98+
- (void)setupPluginName:(NSString *)pluginName {
99+
[self.pluginsMap setValue:pluginName forKey:[pluginName lowercaseString]];
137100
}
138101

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;
141120
}
142121

122+
#pragma mark - dealloc
123+
124+
- (void)dealloc {
125+
[self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"WKJSBridge"];
126+
}
143127

144128
@end

0 commit comments

Comments
 (0)