//npm install jdhybrid_jdbridge
var JDBridge = require("jdhybrid_jdbridge")
使用JDBridge
的API来与原生Native通信。
// 回调function
var callback = function (result) {
showLog('Received result from MyNativePlugin, result = ' + result)
}
// NativePluginName为原生模块名,提供给JS调用
// action为string类型,params可为string或json
JDBridge.callNative('NativePlugin', {action: 'MyAction', params: params, success: callback})
var successCallback = function (result) {
showLog('Received success from MySequenceNativePlugin, result = ' + result)
}
var progressCallback = function (result, response) {
showLog('Received progress from MySequenceNativePlugin, complete = ' + response.complete + ', msg = ' + response.msg + ', result = ' + result)
}
// NativePlugin为原生模块名,提供给JS调用
// 除了上面所示方法传参外,模块名也可使用以下方法传参
JDBridge.callNative({name: 'NativePlugin', params: params, success: successCallback, progress: progressCallback})
若原生注册了默认模块,JS调用时可不指定特定模块名
var callback = function (result, response) {
showLog('Received result from native, complete = ' + response.complete + ', msg = ' + response.msg + ', result = ' + result)
}
JDBridge.callNative({params: params, success: callback})
//JsPluginName为JS模块名,供原生调用
//同步
JDBridge.registerPlugin('JsPluginName', function (params) {
showLog('MySyncJsPlugin invoked by native, params = ' + JSON.stringify(params))
return JSON.stringify(result)
})
//异步
JDBridge.registerPlugin('JsPluginName', function (params, callback) {
showLog('MyAsyncJsPlugin invoked by native, params = ' + JSON.stringify(params))
callback(JSON.stringify(result))
})
//处理失败
JDBridge.registerPlugin('JsPluginName', function (params, callback) {
showLog('MyAsyncJsPlugin invoked by native, params = ' + JSON.stringify(params))
var isSuccess = true;//false
callback(JSON.stringify(result), isSuccess)
})
//JsPluginName为JS模块名,供原生调用
//通过callback返回中间、最后结果给原生,使用complete标志是否完成
JDBridge.registerPlugin('JsPluginName', function (params, callback) {
showLog('MySequenceJsPlugin invoked by native, params = ' + JSON.stringify(params))
var isSuccess = true;//false
var time = 0
var timer = setInterval(function () {
var complete = false
time++
if (time == 10) {
clearInterval(timer)
complete = true
}
callback((time * 10) + '%, MySequenceJsPlugin returns ' + JSON.stringify(result), isSuccess, complete)
}, 500)
})
JDBridge.unregisterPlugin('JsPluginName')
JS可注册默认处理,原生调用时可不指定JS模块名
JDBridge.registerDefaultPlugin(function (params, callback) {
showLog('Default JS plugin invoked by native, params = ' + JSON.stringify(params))
callback('Default JS plugin returns ' + JSON.stringify(params))
})
请等JDBridge初始化好后使用JS功能。可使用以下代码等待初始化。
function connectJDBridge(callback) {
if (window.JDBridge) {
callback(JDBridge)
} else {
window.addEventListener(
'JDBridgeReady',
function () {
callback(JDBridge)
},
false
);
}
}
//run this function
connectJDBridge(function (bridge) {
showLog('JDBridge is ready.')
//do your work with JDBridge
})
默认情况下JDBridge
不会输出debug log,您可以用以下方法展示log。
JDBridge.setDebug(true);
我们基于H5最熟悉的window.addEventListener实现了一套事件通知机制
原生需使用JDWebView
时才可使用
- webView 可见性
window.addEventListener('ContainerShow', function(event){
alert(event);
}, false);
window.addEventListener('ContainerHide', function(event){
alert(event);
}, false);
- App 进入前后台(only iOS)
window.addEventListener('AppShow', function(event){
alert(event);
}, false);
window.addEventListener('AppHide', function(event){
alert(event);
}, false);
window.addEventListener('customEvent', function(event){
alert(JSON.parse(event.params));
}, false);