jsbridge与通信模型
三層通信模型:
應用層、解釋層、會話層;
通信協議:
通信原語;
報文格式;
網絡層:
_evaluateJavascript
會話層:
#define kQueueHasMessage @"__wvjb_queue_message__"
#define kBridgeLoaded @"__bridge_loaded__"
表示層:
NSString* responseId = message[@"responseId"];
WVJBResponseCallback responseCallback = _responseCallbacks[responseId];
應用層:
[_bridge callHandler:@"testJavascriptHandler" data:data responseCallback:^(id response) {
NSLog(@"testJavascriptHandler responded: %@", response);
}];
通信機制:
1、js發送消息
sendMessageQueue.push(message);
messagingIframe.src = CUSTOM_PROTOCOL_SCHEME + '://' + QUEUE_HAS_MESSAGE;
2、oc端接收消息
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
NSString *messageQueueString = [self _evaluateJavascript:[_base webViewJavascriptFetchQueyCommand]];
[_base flushMessageQueue:messageQueueString];
3、oc端消息處理:
WVJBHandler handler = self.messageHandlers[message[@"handlerName"]];
handler(message[@"data"], responseCallback);
構建返回報文并返回:
NSString* callbackId = message[@"callbackId"];
if (callbackId) {
responseCallback = ^(id responseData) {
if (responseData == nil) {
responseData = [NSNull null];
}
WVJBMessage* msg = @{ @"responseId":callbackId, @"responseData":responseData };
[self _queueMessage:msg];
};
}
4、oc端發送消息
NSString* javascriptCommand = [NSString stringWithFormat:@"WebViewJavascriptBridge._handleMessageFromObjC('%@');", messageJSON];
[self _evaluateJavascript:javascriptCommand];
通信原語:通信控制
#define kOldProtocolScheme @"wvjbscheme"
#define kNewProtocolScheme @"https"
#define kQueueHasMessage @"__wvjb_queue_message__"
#define kBridgeLoaded @"__bridge_loaded__"
https://__bridge_loaded__/
https://__wvjb_queue_message__/
通信報文:
{
callbackId = "cb_1_1560136340114";
data = {
foo = bar;
};
handlerName = testObjcCallback;
}
會話維護:
callbackId
responseId
應用協議:
handlerName
生命周期管理
https://__bridge_loaded__/
總結
以上是生活随笔為你收集整理的jsbridge与通信模型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stm32f767中文手册_ALIENT
- 下一篇: java judgefilecode_V