WebViewJavascriptBridge is an iOS/OSX bridge for Obj-C and JavaScript to communicate via UIWebViews/WebViews. If you like WebViewJavascriptBridge, you might also want to check out WebViewProxy. Let me briefly explain the principles of Obj-C and JavaScript. It is very simple for Obj-C to call JavaScript. You can call JavaScript code through the stringByEvaluatingJavaScriptFromString: method of webview; JavaScript calls Obj-C by receiving JavaScript network requests through the web view's proxy method shouldStartLoadWithRequest: to implement the call. Projects using WebViewJavascriptBridge There are many companies and projects using WebViewJavascriptBridge. This list is not complete, feel free to add to it. Facebook Messenger Facebook Paper Yardsale EverTrue Game Insight Altralogica Sush.io Flutterby Labs JD Media's China Dojo4's Imbed CareZone Hemlig Installation & Examples (iOS & OSX) First open the Example Apps folder. Open the iOS or OSX project and click Run. To use WebViewJavascriptBridge in your own project: 1) Drag the WebViewJavascriptBridge folder into your project. In the dialog box that appears, uncheck (I think it should be checked) "Copy items into destination group's folder" and select "Create groups for any folders" 2) Import the header file and declare a property:
3) Instantiate WebViewJavascriptBridge and bring a UIWebView (iOS) or WebView (OSX):
4) First send some messages from ObjC to javascript:
5) Then, look at the javascript side:
Contributors & Forks Contributors: https://github.com/marcuswestin/WebViewJavascriptBridge/graphs/contributors Forks: https://github.com/marcuswestin/WebViewJavascriptBridge/network/members API Reference ObjC API
Create a javascript bridge to the web view. If the javascript needs a response, then WVJBResponseCallback cannot be nil. Of course, you can get the life cycle events of the web view through webViewDelegate:(UIWebViewDelegate*)webViewDelegate. example:
Send a message to javascript. And after the message is sent successfully, you can make some response through the responseCallback block. example:
Register a handler called handlerName. JavaScript can call this handler via WebViewJavascriptBridge.callHandler("handlerName"). example:
Calls the handler named handlerName in javascript. After the handler is successfully called, a response can be made through the responseCallback block. example:
Defining a bundle WebViewJavascriptBridge requires the WebViewJavascriptBridge.js.txt file to be embedded in the web view to create a bridge on the JS side. The standard implementation uses mainBundle to find this file. If you build a static library and you put this file somewhere else, you can find the WebViewJavascriptBridge.js.txt file as follows: [WebViewJavascriptBridge bridgeForWebView: (UIWebView/WebView*)webView webViewDelegate: (UIWebViewDelegate*)webViewDelegate handler:(WVJBHandler)handler resourceBundle:(NSBundle*)bundle example:
Javascript API document.addEventListener('WebViewJavascriptBridgeReady', function onBridgeReady(event) { … }, false) Waiting for the WebViewJavascriptBridgeReady DOM event. example:
bridge.init(function messageHandler(data, response) { … }) Initialize the bridge. This will invoke the 'WebViewJavascriptBridgeReady' event handler. This messageHandler function will receive all messages sent through the ObjC [bridge send:(id)data] and [bridge send:(id)data responseCallback:(WVJBResponseCallback)responseCallback] methods. If there is a WVJBResponseCallback block when ObjC sends a message, the message can be sent through the response object. example:
bridge.send("Hi there!") bridge.send({ Foo:"Bar" }) bridge.send(data, function responseCallback(responseData) { … }) Send a message to ObjC. After the message is sent successfully, you can react through the responseCallback function. example:
bridge.registerHandler(“handlerName”, function(responseData) { … }) Register a handler called handlerName. ObjC can call this handler via [bridge callHandler:"handlerName" data:@"Foo"] and [bridge callHandler:"handlerName" data:@"Foo" responseCallback:^(id responseData) { … }] example:
iOS4 support (including JSONKit) NOTE: iOS4 support has not been tested in V2+. WebViewJavascriptBridge uses NSJSONSerialization by default. If you need iOS 4 support, you can use JSONKit and add the USE_JSONKIT preprocessor macro to your project. |
<<: Cocos Developer Salon Chengdu Station will be grandly opened to serve entrepreneurs
>>: Subversive design - Apple cancels the Home button
Today is World Flu Day. Every autumn and winter, ...
007, the memory of a generation. At that time, ev...
□ Zhu Rongkuan On May 3, the Chang'e-6 probe ...
[[434654]] In the early morning of November 11, A...
As Internet business matures, it has become a con...
Mixed Knowledge Specially designed to cure confus...
On the road of scientific exploration, countless ...
The pain of gout is enough to make an eagle-like ...
The listing of Baofeng Video did not make its fou...
Children are not divided into big or small, we ar...
Not long ago, a somewhat sci-fi news became a hot...
The cute "Jerboa" that appeared in the ...
Qun Xiang "IP Monetization Training Camp 5-6...