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
"Jiang Ziya" is scheduled to be release...
Preface We have talked about the tools for androi...
In the TV series “Nothing But Thirty”, “blue fire...
Resource satellites are China's earliest tran...
my country has the highest prevalence of liver di...
Since the development of the Internet, people hav...
As you age, do you have this problem: when you re...
Cedar is a plant of the genus Cedrus in the Pinac...
According to foreign media reports, if you have s...
If you don’t want to be blamed for “ineffective m...
On the afternoon of June 10, Xiaomi held a press ...
One minute with the doctor, the postures are cons...
Today we are going to talk about this old questio...
Everything will be reset to zero. With the openin...
Talking about data analysis theory alone is too d...