Finally, call setupWebViewJavascriptBridge and then use the bridge to register handlers and call ObjC handlers:
setupWebViewJavascriptBridge(function(bridge){/* Initialize your app here */bridge.registerHandler('JS Echo',function(data,responseCallback){console.log("JS Echo called with:",data)responseCallback(data)})bridge.callHandler('ObjC Echo',{'key':'value'},functionresponseCallback(responseData){console.log("JS received response:",responseData)})})
Automatic reference counting (ARC)
This library relies on ARC, so if you use ARC in you project, all works fine.
But if your project have no ARC support, be sure to do next steps:
In your Xcode project open project settings -> 'Build Phases'
Expand 'Compile Sources' header and find all *.m files which are belongs to this library. Make attention on the 'Compiler Flags' in front of each source file in this list
For each file add '-fobjc-arc' flag
Now all WVJB files will be compiled with ARC support.
Call the javascript handler called handlerName. If a responseCallback block is given the javascript handler can respond.
Example:
[self.bridge callHandler:@"showAlert"data:@"Hi from ObjC to JS!"];
[self.bridge callHandler:@"getCurrentPageUrl"data:nilresponseCallback:^(id responseData) {
NSLog(@"Current UIWebView page URL is: %@", responseData);
}];
[bridge setWebViewDelegate:(id)webViewDelegate]
Optionally, set a WKNavigationDelegate/UIWebViewDelegate if you need to respond to the web view's lifecycle events.
[bridge disableJavscriptAlertBoxSafetyTimeout]
UNSAFE. Speed up bridge message passing by disabling the setTimeout safety check. It is only safe to disable this safety check if you do not call any of the javascript popup box functions (alert, confirm, and prompt). If you call any of these functions from the bridged javascript code, the app will hang.
Register a handler called handlerName. The ObjC can then call this handler with [bridge callHandler:"handlerName" data:@"Foo"] and [bridge callHandler:"handlerName" data:@"Foo" responseCallback:^(id responseData) { ... }]
请发表评论