this article reprinted to http://blog.csdn.net/remote_roamer/article/details/7261490official website of the Webviewjavascriptbridge project
Https://github.com/marcuswestin/WebViewJavascriptBridge
Main process:
- Download the WebViewJavascriptBridge.h and WEBVIEWJAVASCRIPTBRIDGE.M files into your own project and add them to the project.
- Used in the corresponding. h file
#import "WebViewJavascriptBridge.h"
- use category in the corresponding. h file; webviewjavascriptbridgedelegate [cpp] view plaincopy Object Id= "Zeroclipboardmovie_1" Data= "http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" width = "align=" middle "type=" Application/x-shockwave-flash "height=" bgcolor= "#ffffff" >< param name= "allowfullscreen" value= "false" >
Li class= "alt" > @interface HtmlViewController : UIViewController< webviewjavascriptbridgedelegate>
- How to accept messages from the JS program in HTML
- The following methods are implemented: (delegate settings are put into the viewdidload method, but not into
Initwithnibname
method)
[CPP]View Plaincopy< param name= "allowfullscreen" value= "false" >
- -(void) viewdidload
- {
- Self.javascriptbridge = [Webviewjavascriptbridge javascriptbridgewithdelegate:self];
- Self.webView.delegate = Self.javascriptbridge;
- [Super Viewdidload];
- }
[CPP]View Plaincopy< param name= "allowfullscreen" value= "false" >
- -(void) Javascriptbridge: (Webviewjavascriptbridge *) Bridge Receivedmessage: (NSString *) Message Fromwebview: (UIWebView *) WebView
- {
- NSLog (@"Myjavascriptbridgedelegate received message:%@", message);
- }
- Send and receive messages in HTML using JavaScript methods:[HTML]View Plaincopy
- Document.addeventlistener (' Webviewjavascriptbridgeready ', function Onbridgeready () {
- Webviewjavascriptbridge.setmessagehandler (function (message) {
- Alert (' Received message: ' + message)
- })
- Webviewjavascriptbridge.sendmessage (' Hello from the JavaScript, send message to OBJC ')
- }, False)
- The OBJECTC program sends messages to HTML.
- In the business of the program, use the following methods[CPP]View Plaincopy
- //Send message to HTML
- [Self.javascriptbridge sendmessage:@"Well hello THERE,OBJC sends a message to HTML" ToWebView:self.webView];
The following blog article, is not through the Webviewjavascriptbridge components to deal with OBJECTC and HTML communication, from the principle will be clearer to help us understand
http://wangjun.easymorse.com/?p=1087
This article is also described in detail
http://blog.csdn.net/cococoolwhj/article/details/7019828
Use this method when the JS file inside if the Chinese. To use JS inside the method encodeURI to deal with. Otherwise, Xcode uses
NSString* rurl=[[[request URL] absolutestring] Stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding]; Unable to get Chinese
var url="protocol://"+encodeURI($ ("#textarea"). Val ());