Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test add send method #355

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Example Apps/ExampleApp.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,71 @@
button { margin:0 3px 10px; font-size:12px; }
.logLine { border-bottom:1px solid #ccc; padding:4px 2px; font-family:courier; font-size:11px; }
</style>
<script type="text/javascript"
src="https://res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/vconsole/3.0.0/vconsole.min.js"></script>
<script>
var vConsole = new VConsole();
</script>
</head><body>
<h1>WebViewJavascriptBridge Demo</h1>
<!--测试-->
<p>
<input type="text" id="text1" value="用户名(username)"/>
</p>
<p>
<input type="text" id="text2" value="password"/>
</p>
<p>
<xmp id="show">
</xmp>
</p>
<p>
<input type="button" id="enter" value="发消息给Native" onclick="testClick();"
/>
</p>
<p>
<input type="button" id="enter1" value="调用Native方法" onclick="testClick1();"
/>
</p>

<script>
window.onerror = function(err) {
log('window.onerror: ' + err)
}

<!--发送消息给native-->
function testClick() {
var str1 = document.getElementById("text1").value;
var str2 = document.getElementById("text2").value;

//send message to native
var data = {id: 1, content: "js 发送handleName=nil的方法"};
window.WebViewJavascriptBridge
.send(data, function(responseData) {
document.getElementById("show").innerHTML = "repsonseData from java, data " + responseData
});
}

<!--调用Native的方法-->
function testClick1() {
var str1 = document.getElementById("text1").value;
var str2 = document.getElementById("text2").value;

//调用Native方法
window.WebViewJavascriptBridge.callHandler('testObjcCallback', {'param': '中文测试'}, function(responseData) {
document.getElementById("show").innerHTML = "send get responseData from java, data = " + responseData
});
}

function setupWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
document.addEventListener(
'WebViewJavascriptBridgeReady'
, function() {
callback(WebViewJavascriptBridge)
},
false
);
if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
window.WVJBCallbacks = [callback];
var WVJBIframe = document.createElement('iframe');
Expand Down
3 changes: 3 additions & 0 deletions WebViewJavascriptBridge/WKWebViewJavascriptBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
+ (instancetype)bridgeForWebView:(WKWebView*)webView;
+ (void)enableLogging;

- (void)send:(id)data;
- (void)send:(id)data responseCallback:(WVJBResponseCallback)responseCallback;

- (void)registerHandler:(NSString*)handlerName handler:(WVJBHandler)handler;
- (void)removeHandler:(NSString*)handlerName;
- (void)callHandler:(NSString*)handlerName;
Expand Down
3 changes: 3 additions & 0 deletions WebViewJavascriptBridge/WebViewJavascriptBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
+ (void)enableLogging;
+ (void)setLogMaxLength:(int)length;

- (void)send:(id)data;
- (void)send:(id)data responseCallback:(WVJBResponseCallback)responseCallback;

- (void)registerHandler:(NSString*)handlerName handler:(WVJBHandler)handler;
- (void)removeHandler:(NSString*)handlerName;
- (void)callHandler:(NSString*)handlerName;
Expand Down
26 changes: 26 additions & 0 deletions WebViewJavascriptBridge/WebViewJavascriptBridge_JS.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
}
}
window.WebViewJavascriptBridge = {
init:init,
send:send,
registerHandler: registerHandler,
callHandler: callHandler,
disableJavscriptAlertBoxSafetyTimeout: disableJavscriptAlertBoxSafetyTimeout,
Expand All @@ -35,6 +37,8 @@

var messagingIframe;
var sendMessageQueue = [];
//接收消息队列
var receiveMessageQueue = [];
var messageHandlers = {};

var CUSTOM_PROTOCOL_SCHEME = 'https';
Expand All @@ -58,6 +62,23 @@ function callHandler(handlerName, data, responseCallback) {
function disableJavscriptAlertBoxSafetyTimeout() {
dispatchMessagesWithTimeoutSafety = false;
}
// 设置默认消息线程
function init(messageHandler) {
if (window.WebViewJavascriptBridge) {
throw new error('WebViewJavascriptBridge.init called twice');
}
window.WebViewJavascriptBridge._messageHandler = messageHandler;
var receiveMessages = receiveMessageQueue;
receiveMessageQueue = null;
for (var i = 0; i < receiveMessages.length; i++) {
_handleMessageFromObjC(receiveMessages[i]);
}
}

// 发送默认消息
function send(message, responseCallback) {
_doSend({data: message}, responseCallback);
}

function _doSend(message, responseCallback) {
if (responseCallback) {
Expand Down Expand Up @@ -113,6 +134,11 @@ function _doDispatchMessageFromObjC() {
}

function _handleMessageFromObjC(messageJSON) {
//加入接收消息队列
console.log(messageJSON);
if (receiveMessageQueue) {
receiveMessageQueue.push(messageJSON);
}
_dispatchMessageFromObjC(messageJSON);
}

Expand Down