forked from marcuswestin/WebViewJavascriptBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebViewJavascriptBridge.m
More file actions
executable file
·198 lines (166 loc) · 8.1 KB
/
Copy pathWebViewJavascriptBridge.m
File metadata and controls
executable file
·198 lines (166 loc) · 8.1 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#import "WebViewJavascriptBridge.h"
#ifdef USE_JSONKIT
#import "JSONKit.h"
#endif
@interface WebViewJavascriptBridge ()
@property (nonatomic,strong) NSMutableArray *startupMessageQueue;
@property (nonatomic,strong) NSMutableDictionary *responseCallbacks;
@property (nonatomic,strong) NSMutableDictionary *messageHandlers;
@property (atomic,assign) NSInteger uniqueId;
@property (nonatomic, strong) UIWebView* webView;
@property (nonatomic, strong) id <UIWebViewDelegate> webViewDelegate;
@property (nonatomic, copy) WVJBHandler messageHandler;
- (void)_flushMessageQueue;
- (void)_sendData:(NSDictionary*)data responseCallback:(WVJBResponseCallback)responseCallback handlerName:(NSString*)handlerName;
- (void)_queueMessage:(NSDictionary*)message;
- (void)_dispatchMessage:(NSDictionary*)message;
@end
@implementation WebViewJavascriptBridge
static NSString *MESSAGE_SEPARATOR = @"__WVJB_MESSAGE_SEPERATOR__";
static NSString *CUSTOM_PROTOCOL_SCHEME = @"wvjbscheme";
static NSString *QUEUE_HAS_MESSAGE = @"__WVJB_QUEUE_MESSAGE__";
+ (id)javascriptBridgeForWebView:(UIWebView *)webView handler:(WVJBHandler)handler {
return [self javascriptBridgeForWebView:webView webViewDelegate:nil handler:handler];
}
+ (id)javascriptBridgeForWebView:(UIWebView *)webView webViewDelegate:(id<UIWebViewDelegate>)webViewDelegate handler:(WVJBHandler)messageHandler {
WebViewJavascriptBridge* bridge = [[WebViewJavascriptBridge alloc] init];
bridge.messageHandler = messageHandler;
bridge.startupMessageQueue = [NSMutableArray array];
bridge.responseCallbacks = [NSMutableDictionary dictionary];
bridge.messageHandlers = [NSMutableDictionary dictionary];
bridge.uniqueId = 0;
bridge.webView = webView;
bridge.webViewDelegate = webViewDelegate;
webView.delegate = bridge;
return bridge;
}
- (void)send:(NSDictionary *)data {
[self send:data responseCallback:nil];
}
- (void)send:(NSDictionary *)data responseCallback:(WVJBResponseCallback)responseCallback {
[self _sendData:data responseCallback:responseCallback handlerName:nil];
}
- (void)callHandler:(NSString *)handlerName {
[self callHandler:handlerName data:nil responseCallback:nil];
}
- (void)callHandler:(NSString *)handlerName data:(id)data {
[self callHandler:handlerName data:data responseCallback:nil];
}
- (void)callHandler:(NSString *)handlerName data:(id)data responseCallback:(WVJBResponseCallback)responseCallback {
[self _sendData:data responseCallback:responseCallback handlerName:handlerName];
}
- (void)registerHandler:(NSString *)handlerName handler:(WVJBHandler)handler {
[self.messageHandlers setObject:handler forKey:handlerName];
}
- (void)_sendData:(NSDictionary *)data responseCallback:(WVJBResponseCallback)responseCallback handlerName:(NSString*)handlerName {
NSMutableDictionary* message = [NSMutableDictionary dictionaryWithObject:data forKey:@"data"];
if (responseCallback) {
NSString* callbackId = [NSString stringWithFormat:@"objc_cb_%d", ++_uniqueId];
[self.responseCallbacks setObject:responseCallback forKey:callbackId];
[message setObject:callbackId forKey:@"callbackId"];
}
if (handlerName) {
[message setObject:handlerName forKey:@"handlerName"];
}
[self _queueMessage:message];
}
- (void)_queueMessage:(NSDictionary *)message {
if (self.startupMessageQueue) {
[self.startupMessageQueue addObject:message];
} else {
[self _dispatchMessage:message];
}
}
- (void)_dispatchMessage:(NSDictionary *)message {
#ifdef USE_JSONKIT
NSString *messageJSON = [message JSONString];
#else
NSString *messageJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:message options:0 error:nil] encoding:NSUTF8StringEncoding];
#endif
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\r" withString:@"\\r"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\f" withString:@"\\f"];
[_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"WebViewJavascriptBridge._handleMessageFromObjC('%@');", messageJSON]];
}
- (void)_flushMessageQueue {
NSString *messageQueueString = [_webView stringByEvaluatingJavaScriptFromString:@"WebViewJavascriptBridge._fetchQueue();"];
NSArray* messages = [messageQueueString componentsSeparatedByString:MESSAGE_SEPARATOR];
for (NSString *messageJSON in messages) {
// normal message - pass to bridge
#ifdef USE_JSONKIT
NSDictionary *message = [messageJSON objectFromJSONString];
#else
NSDictionary *message = [NSJSONSerialization JSONObjectWithData:[messageJSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
#endif
WVJBResponseCallback responseCallback = NULL;
if ([message objectForKey:@"callbackId"]) {
__block NSString* responseId = [message objectForKey:@"callbackId"];
responseCallback = ^(NSDictionary* data) {
NSDictionary* responseMessage = [NSDictionary dictionaryWithObjectsAndKeys: responseId, @"responseId", data, @"data", nil];
[self _queueMessage:responseMessage];
};
}
WVJBHandler handler = self.messageHandler;
if ([message objectForKey:@"handlerName"]) {
handler = [self.messageHandlers objectForKey:[message objectForKey:@"handlerName"]];
} else if ([message objectForKey:@"responseId"]) {
handler = [self.responseCallbacks objectForKey:[message objectForKey:@"responseId"]];
}
@try {
handler([message objectForKey:@"data"], responseCallback);
}
@catch (NSException *exception) {
NSLog(@"WebViewJavascriptBridge: WARNING: handler threw. %@ %@", message, exception);
}
}
}
#pragma mark UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (webView != _webView) { return; }
if (![[_webView stringByEvaluatingJavaScriptFromString:@"typeof WebViewJavascriptBridge == 'object'"] isEqualToString:@"true"]) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"WebViewJavascriptBridge.js" ofType:@"txt"];
NSString *js = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[_webView stringByEvaluatingJavaScriptFromString:js];
}
if (self.startupMessageQueue) {
for (id queuedMessage in self.startupMessageQueue) {
[self _dispatchMessage:queuedMessage];
}
self.startupMessageQueue = nil;
}
if (self.webViewDelegate) {
[self.webViewDelegate webViewDidFinishLoad:webView];
}
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
if (webView != _webView) { return; }
if (self.webViewDelegate) {
[self.webViewDelegate webView:_webView didFailLoadWithError:error];
}
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (webView != _webView) { return YES; }
NSURL *url = [request URL];
if ([[url scheme] isEqualToString:CUSTOM_PROTOCOL_SCHEME]) {
if ([[url host] isEqualToString:QUEUE_HAS_MESSAGE]) {
[self _flushMessageQueue];
} else {
NSLog(@"WebViewJavascriptBridge: WARNING: Received unknown WebViewJavascriptBridge command %@://%@", CUSTOM_PROTOCOL_SCHEME, [url path]);
}
return NO;
} else if (self.webViewDelegate) {
return [self.webViewDelegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
} else {
return YES;
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
if (webView != _webView) { return; }
if (self.webViewDelegate) {
[self.webViewDelegate webViewDidStartLoad:webView];
}
}
@end