Skip to content

Commit f09c53b

Browse files
committed
[RC] 初步hybrid方案
1 parent 5040d85 commit f09c53b

File tree

109 files changed

+5283
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+5283
-0
lines changed

CSJSBridge/CSBridgeConstant.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// CSBridgeConstant.h
3+
// CSWebViewJavascriptBridge_Example
4+
//
5+
// Created by 余强 on 2018/8/30.
6+
// Copyright © 2018年 [email protected]. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
extern NSString *const CSJSContext;
12+
extern NSString *const CSJSContext;
13+
extern NSString *const CSJSNativeObject;
14+
extern NSString *const CSJSApiVersion;
15+
16+
/*
17+
用来存储native主动调js所需要保存的function的handler
18+
*/
19+
extern NSString *const CSJSNativeCallWebFunctionHandler;
20+
21+
extern NSString *const CSJSCallbackFunction;
22+
23+
extern NSString *const CSJSNativeCallWebFunction;
24+
25+
extern NSString *const CSJSAPIVersion;
26+
27+
//EVENT
28+
//native Event
29+
extern NSString *const CSJSWebViewWillAppearEvent;
30+
31+
extern NSString *const CSJSWebViewDidAppearEvent;
32+
33+
extern NSString *const CSJSWebViewWillDisappearEvent;
34+
35+
extern NSString *const CSJSWebViewDisappearEvent;
36+
37+
//application event
38+
39+
@interface CSBridgeConstant : NSObject
40+
41+
@end

CSJSBridge/CSBridgeConstant.m

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// CSBridgeConstant.m
3+
// CSWebViewJavascriptBridge_Example
4+
//
5+
// Created by 余强 on 2018/8/30.
6+
// Copyright © 2018年 [email protected]. All rights reserved.
7+
//
8+
9+
#import "CSBridgeConstant.h"
10+
11+
NSString *const CSJSContext = @"documentView.webView.mainFrame.javaScriptContext";
12+
NSString *const CSJSAction = @"action";
13+
NSString *const CSJSNativeObject = @"CSJSWebViewBridgeCore";
14+
NSString *const CSJSApiVersion = @"10";
15+
NSString *const CSJSNativeCallWebFunctionHandler = @"nativeCallWebFunction";
16+
NSString *const CSJSAPIVersion = @"CSJSAPIVersion";
17+
18+
NSString *const CSJSCallbackFunction = @"jsBridge.callbackWeb";
19+
20+
NSString *const CSJSNativeCallWebFunction = @"jsBridge.nativeCallWeb";
21+
22+
//native Event
23+
NSString *const CSJSWebViewWillAppearEvent = @"viewWillAppear";
24+
25+
NSString *const CSJSWebViewDidAppearEvent = @"viewDidAppear";
26+
27+
NSString *const CSJSWebViewWillDisappearEvent = @"viewWillDisappear";
28+
29+
NSString *const CSJSWebViewDisappearEvent = @"viewDidDisappear";
30+
31+
32+
@implementation CSBridgeConstant
33+
34+
35+
@end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// CSJSBridgeActionManager.h
3+
// CSWebViewJavascriptBridge_Example
4+
//
5+
// Created by 余强 on 2018/8/29.
6+
// Copyright © 2018年 [email protected]. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "CSJSHandlerActionProtocol.h"
11+
#import "CSWebViewJavascriptBridge.h"
12+
13+
@interface CSJSBridgeActionHandlerManager : NSObject
14+
15+
@property (nonatomic,strong) __kindof CSWebViewJavascriptBridge *bridge;
16+
17+
+ (instancetype)shareManager;
18+
19+
+ (id<CSJSHandlerProtocol>)handlerWithName:(NSString *)name;
20+
21+
- (void)callHandler:(NSString *)handlerName message:(CSJSMessage *)message JSCallBackBlock:(CSJSCallBackBlock)jsCallBackBlock;
22+
23+
- (void)callJS:(NSString *)action
24+
message:(CSJSMessage *)message
25+
JSCompletionBlock:(CSJSCompletionBlock)jsCompletionBlock;
26+
27+
@end
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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

CSJSBridge/CSJSMessage.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// CSJSMessage.h
3+
// CSWebViewJavascriptBridge_Example
4+
//
5+
// Created by 余强 on 2018/8/29.
6+
// Copyright © 2018年 [email protected]. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface CSJSMessage: NSObject
12+
13+
14+
/**
15+
JS主动调native逻辑,分模块定义业务,common,xx,yy等,一个handler代表一个模块
16+
*/
17+
@property (nonatomic, copy, readonly) NSString *handler;
18+
19+
/**
20+
模块handler中的action事件,多个action构成一个handler
21+
*/
22+
@property (nonatomic, copy, readonly) NSString *action;
23+
24+
/**
25+
传递的数据
26+
*/
27+
@property (nonatomic, strong) NSDictionary *data;
28+
/**
29+
回调id标记
30+
*/
31+
@property (nonatomic, copy, readonly) NSString *callbackID;
32+
/**
33+
JS主动调native,native执行完相关业务后回调JS的函数,该function可写死,也可动态,如"jsBridge.callbackWeb"
34+
*/
35+
@property (nonatomic, copy, readonly) NSString *callbackFunction;
36+
/**
37+
native主动调用JS的函数,该function可写死,也可动态,"jsBridge.nativeCallWeb"
38+
*/
39+
@property (nonatomic, copy, readonly) NSString *nativeCallWebFunction;
40+
41+
/**
42+
JSAPI版本号,用来区别不同版本之间JSAPI的不同
43+
*/
44+
@property (nonatomic, copy) NSString *JSAPIVersion;
45+
46+
+ (instancetype)messageWithDictionary:(NSDictionary *)dic;
47+
48+
- (NSString *)toJavascriptMessage;
49+
50+
- (NSDictionary *)toDictionary;
51+
52+
@end

CSJSBridge/CSJSMessage.m

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//
2+
// CSJSMessage.m
3+
// CSWebViewJavascriptBridge_Example
4+
//
5+
// Created by 余强 on 2018/8/29.
6+
// Copyright © 2018年 [email protected]. All rights reserved.
7+
//
8+
9+
#import "CSJSMessage.h"
10+
11+
@implementation CSJSMessage
12+
13+
+ (instancetype)messageWithDictionary:(NSDictionary *)dic
14+
{
15+
return [[self alloc] initWithDictionary:dic];
16+
}
17+
18+
- (NSDictionary *)toDictionary
19+
{
20+
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
21+
_handler ? [dic setObject:_handler forKey:@"handler"] : nil;
22+
_action ? [dic setObject:_action forKey:@"action"] : nil;
23+
_callbackFunction ? [dic setObject:_callbackFunction forKey:@"callbackFunction"] : nil;
24+
_nativeCallWebFunction ? [dic setObject:_nativeCallWebFunction forKey:@"nativeCallWebFunction"] : nil;
25+
_callbackID ? [dic setObject:_callbackID forKey:@"callbackID"] : nil;
26+
_data ? [dic setObject:_data forKey:@"data"] : nil;
27+
_JSAPIVersion ? [dic setObject:_JSAPIVersion forKey:@"JSAPIVersion"] : nil;
28+
return dic;
29+
}
30+
31+
// JSON Javascript编码处理
32+
- (NSString *)toJavascriptMessage
33+
{
34+
NSString *message = [self toJsonString];
35+
[message stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
36+
message = [message stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
37+
message = [message stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"];
38+
message = [message stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
39+
message = [message stringByReplacingOccurrencesOfString:@"\r" withString:@"\\r"];
40+
message = [message stringByReplacingOccurrencesOfString:@"\f" withString:@"\\f"];
41+
message = [message stringByReplacingOccurrencesOfString:@"\u2028" withString:@"\\u2028"];
42+
message = [message stringByReplacingOccurrencesOfString:@"\u2029" withString:@"\\u2029"];
43+
44+
return message;
45+
}
46+
47+
- (NSString *)toJsonString
48+
{
49+
NSDictionary *dic = [self toDictionary];
50+
return [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
51+
}
52+
53+
- (instancetype)initWithDictionary:(NSDictionary *)dic
54+
{
55+
self = [super init];
56+
if (self) {
57+
_handler = dic[@"handler"];
58+
_action = dic[@"action"];
59+
_callbackFunction = dic[@"callbackFunction"];
60+
_nativeCallWebFunction = dic[@"nativeCallWebFunction"];
61+
_callbackID = dic[@"callbackID"];
62+
_data = dic[@"data"];
63+
_JSAPIVersion = dic[@"JSAPIVersion"];
64+
}
65+
return self;
66+
}
67+
68+
@end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// CSUIWebViewJavascriptBridge.h
3+
// CSWebViewJavascriptBridge_Example
4+
//
5+
// Created by 余强 on 2018/8/29.
6+
// Copyright © 2018年 [email protected]. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import <JavaScriptCore/JavaScriptCore.h>
11+
#import "CSWebViewJavascriptBridge.h"
12+
13+
/**
14+
CSUIWebViewJavascriptBridge为CSWebViewJavascriptBridge的subClass,为UIWebView提供桥接能力
15+
*/
16+
@protocol CSJavaScriptProtocolExport <JSExport>
17+
18+
//UIWebView时候与JS约定的JS调native的最底层方法
19+
- (void)callApp:(id)message;
20+
21+
@end
22+
23+
@interface CSUIWebViewJavascriptBridge : CSWebViewJavascriptBridge<CSWebViewJavascriptBridgeProtocol>
24+
25+
@end

0 commit comments

Comments
 (0)