|
| 1 | +// |
| 2 | +// CSJSBridgeActionManager.m |
| 3 | +// CSWebViewJavascriptBridge_Example |
| 4 | +// |
| 5 | +// Created by 余强 on 2018/8/29. |
| 6 | +// Copyright © 2018年 [email protected]. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "CSJSBridgeActionHandlerManager.h" |
| 10 | +#import "CSJSHandler.h" |
| 11 | + |
| 12 | +@interface CSJSBridgeActionHandlerManager() |
| 13 | + |
| 14 | +/** |
| 15 | + JS主动调OC,native端支持的handler,从CSJSSupportHandlers.plist获取所有handler的配置 |
| 16 | + */ |
| 17 | +@property (nonatomic,strong) NSMutableDictionary <NSString *,id<CSJSHandlerProtocol>>*supportJSHandlersMap; |
| 18 | + |
| 19 | +@end |
| 20 | + |
| 21 | +@implementation CSJSBridgeActionHandlerManager |
| 22 | + |
| 23 | +- (instancetype)init |
| 24 | +{ |
| 25 | + self = [super init]; |
| 26 | + if (self) { |
| 27 | + |
| 28 | + } |
| 29 | + return self; |
| 30 | +} |
| 31 | + |
| 32 | ++ (instancetype)shareManager |
| 33 | +{ |
| 34 | + static CSJSBridgeActionHandlerManager *manager = nil; |
| 35 | + static dispatch_once_t onceToken; |
| 36 | + dispatch_once(&onceToken, ^{ |
| 37 | + manager = [[self alloc] init]; |
| 38 | + }); |
| 39 | + return manager; |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +/** |
| 44 | + 从CSJSSupportHandlers.plist加载所有JS调native所有handler的,并且通过handlerName,handler映射到内存中,通过动态调用handle模块 |
| 45 | +
|
| 46 | + @return <#return value description#> |
| 47 | + */ |
| 48 | +- (NSMutableDictionary <NSString *,id<CSJSHandlerProtocol>>*)supportJSHandlersMap |
| 49 | +{ |
| 50 | + if (!_supportJSHandlersMap) { |
| 51 | + _supportJSHandlersMap = [NSMutableDictionary dictionaryWithCapacity:1]; |
| 52 | + NSString *mapPath = [[NSBundle mainBundle] pathForResource:@"CSJSSupportHandlers" ofType:@"plist"]; |
| 53 | + NSParameterAssert(mapPath); |
| 54 | + NSDictionary *map = [NSDictionary dictionaryWithContentsOfFile:mapPath]; |
| 55 | + [map enumerateKeysAndObjectsUsingBlock:^(NSString* _Nonnull handlerName, NSString * _Nonnull handlerClassString, BOOL * _Nonnull stop) { |
| 56 | + Class handlerClass = NSClassFromString(handlerClassString); |
| 57 | + id handler = [[handlerClass alloc] init]; |
| 58 | + if(!handler) |
| 59 | + { |
| 60 | + NSLog(@"<< handlerClass not exist:%@ >>",handlerClassString); |
| 61 | + } |
| 62 | + else{ |
| 63 | + if ([handler conformsToProtocol:@protocol(CSJSHandlerProtocol)]) { |
| 64 | + //registerHandler: |
| 65 | + [_supportJSHandlersMap setObject:handler forKey:handlerName]; |
| 66 | + CSJSHandler *jsHandler = (CSJSHandler *)handler; |
| 67 | + jsHandler.handlerName = handlerName; |
| 68 | + }else{ |
| 69 | + NSLog(@"<< handler:%@ not conform to protocol:%@ >>",handler, NSStringFromProtocol(@protocol(CSJSHandlerProtocol))); |
| 70 | + } |
| 71 | + } |
| 72 | + }]; |
| 73 | + NSLog(@"<< supportJsHandlerMap:%@ >>",_supportJSHandlersMap); |
| 74 | + } |
| 75 | + return _supportJSHandlersMap; |
| 76 | +} |
| 77 | + |
| 78 | +/* |
| 79 | + 去掉load中handler自注册的方式,改用依靠plist做主动注册的方式 |
| 80 | +- (void)registerHandler:(NSString *)handlerName handler:(id<CSJSHandlerProtocol>)handler |
| 81 | +{ |
| 82 | + [self.supportJSHandlersMap setObject:handler forKey:handlerName]; |
| 83 | +} |
| 84 | + */ |
| 85 | + |
| 86 | +- (void)callHandler:(NSString *)handlerName |
| 87 | + message:(CSJSMessage *)message |
| 88 | + JSCallBackBlock:(CSJSCallBackBlock)jsCallBackBlock |
| 89 | +{ |
| 90 | + id <CSJSHandlerProtocol>handler = self.supportJSHandlersMap[handlerName]; |
| 91 | + if (!handler) { |
| 92 | + NSLog(@"<< error,handler:%@ not register >>",handlerName); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + if([handler respondsToSelector:@selector(callAppAction:message:jsCallBackBlock:)]) |
| 97 | + { |
| 98 | + [handler callAppAction:message.action message:message jsCallBackBlock:jsCallBackBlock]; |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + NSLog(@"<< error,handler not respondsToSelector 'callAppActionWithMessage: '>>"); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +- (void)callJS:(NSString *)action |
| 108 | + message:(CSJSMessage *)message |
| 109 | +JSCompletionBlock:(CSJSCompletionBlock)jsCompletionBlock |
| 110 | +{ |
| 111 | + [self.bridge callJS:action message:message JSCompletionBlock:jsCompletionBlock]; |
| 112 | +} |
| 113 | + |
| 114 | ++ (id<CSJSHandlerProtocol>)handlerWithName:(NSString *)name |
| 115 | +{ |
| 116 | + NSParameterAssert(name.length > 0); |
| 117 | + NSMutableDictionary *supportJSHandlersMap = [[self shareManager] supportJSHandlersMap]; |
| 118 | + id <CSJSHandlerProtocol> handler = supportJSHandlersMap[name]; |
| 119 | + NSParameterAssert(handler != nil); |
| 120 | + if (handler) { |
| 121 | + return handler; |
| 122 | + } |
| 123 | + return nil; |
| 124 | +} |
| 125 | + |
| 126 | +@end |
0 commit comments