Modern cross-platform JavaScript bridge, through which you can invoke each other's functions synchronously or asynchronously between JavaScript and native applications.
DSBridge v3.0 is a milestone version. Compared with v2.0, we have made a lot of changes. Note that v3.0 is incompatible with v2.0, but v2.0 will continue to maintain. If you are a new user, use >=v3.0.
Call Native API and register a javascript API for Native invocation
//Call synchronously varstr=dsBridge.call("testSyn","testSyn");//Call asynchronouslydsBridge.call("testAsyn","testAsyn",function(v){alert(v);})//Register javascript API for NativedsBridge.register('addValue',function(l,r){returnl+r;})
Namespaces can help you better manage your APIs, which is very useful in hybrid applications, because these applications have a large number of APIs. DSBridge (>= v3.0.0) allows you to classify API with namespace. And the namespace can be multilevel, between different levels with '.' division.
Debug mode
In debug mode, some errors will be prompted by a popup dialog , and the exception caused by the native APIs will not be captured to expose problems. We recommend that the debug mode be opened at the development stage. You can open debug mode :
// open debug mode
[dwebview setDebugMode:true];
Progress Callback
Normally, when a API is called to end, it returns a result, which corresponds one by one. But sometimes a call need to repeatedly return multipule times, Suppose that on the Native side, there is a API to download the file, in the process of downloading, it will send the progress information to Javascript many times, then Javascript will display the progress information on the H5 page. Oh...You will find it is difficult to achieve this function. Fortunately, DSBridge supports Progress Callback. You can be very simple and convenient to implement a call that needs to be returned many times. Here's an example of a countdown:
For the complete sample code, please refer to the demo project.
Javascript popup box
For Javascript popup box functions (alert/confirm/prompt), DSBridge has implemented them all by default. the default dialog label text language is Chinese, you can custom the text by calling customJavascriptDialogLabelTitles. If you still want to implement them by yourself , set the DSUIDelegate property which is a proxy of WKUIDelegate.
Note That the default dialog box implemented by DSBridge is modal. This will block the UI thread. If you need modeless, please refer to disableJavascriptDialogBlock.
WKUIDelegate
In DWKWebView , please use DSUIDelegate instead of UIDelegate , because UIDelegate has already been set inner DWKWebView , DSUIDelegate is a proxy of UIDelegate .
API Reference
Object-C API
In Object-c, the object that implements the javascript interfaces is called Object-c API object.
// call echo.synvarret=dsBridge.call("echo.syn",{msg:" I am echoSyn call",tag:1})alert(JSON.stringify(ret))// call echo.asyndsBridge.call("echo.asyn",{msg:" I am echoAsyn call",tag:2},function(ret){alert(JSON.stringify(ret));})
removeJavascriptObject:(NSString *) namespace
Remove the Object-c API object with supplied namespace.
Call the javascript API. If a completionHandler is given, the javascript handler can respond. the methodName can contain the namespace. The completionHandler will be called in main thread.
Example:
[dwebview callHandler:@"append"arguments:@[@"I",@"love",@"you"]
completionHandler:^(NSString * _Nullable value) {
NSLog(@"call succeed, append string is: %@",value);
}];
// call with namespace 'syn', More details to see the Demo project
[dwebview callHandler:@"syn.getInfo"completionHandler:^(NSDictionary * _Nullable value) {
NSLog(@"Namespace syn.getInfo: %@",value);
}];
disableJavascriptDialogBlock:(bool) disable
BE CAREFUL to use. if you call any of the javascript popup box functions (alert, confirm, and prompt), the app will hang, and the javascript execution flow will be blocked. if you don't want to block the javascript execution flow, call this method, the popup box functions will return immediately( confirm return true, and the prompt return empty string).
Example:
[dwebview disableJavascriptDialogBlock:true]
if you want to enable the block, just calling this method with the argument value false .
// test if javascript method exists.
[dwebview hasJavascriptMethod:@"addValue"methodExistCallback:^(bool exist) {
NSLog(@"method 'addValue' exist : %d",exist);
}];
setDebugMode:(bool) debug
Set debug mode. if in debug mode, some errors will be prompted by a popup dialog , and the exception caused by the native APIs will not be captured to expose problems. We recommend that the debug mode be opened at the development stage.
请发表评论