IPhone and ipad iOS development leverages Webviewjavascriptbridge components to communicate HTML in two-way via UIWebView

Source: Internet
Author: User

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:

  1. Download the WebViewJavascriptBridge.h and WEBVIEWJAVASCRIPTBRIDGE.M files into your own project and add them to the project.
  2. Used in the corresponding. h file
    #import "WebViewJavascriptBridge.h"
  3. 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>  
  4. How to accept messages from the JS program in HTML
    1. 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" >
        1. -(void) viewdidload
        2. {
        3. Self.javascriptbridge = [Webviewjavascriptbridge javascriptbridgewithdelegate:self];
        4. Self.webView.delegate = Self.javascriptbridge;
        5. [Super Viewdidload];
        6. }



      [CPP]View Plaincopy< param name= "allowfullscreen" value= "false" >
        1. -(void) Javascriptbridge: (Webviewjavascriptbridge *) Bridge Receivedmessage: (NSString *) Message Fromwebview: (UIWebView *) WebView
        2. {
        3. NSLog (@"Myjavascriptbridgedelegate received message:%@", message);
        4. }
    2. Send and receive messages in HTML using JavaScript methods:[HTML]View Plaincopy
      1. Document.addeventlistener (' Webviewjavascriptbridgeready ', function Onbridgeready () {
      2. Webviewjavascriptbridge.setmessagehandler (function (message) {
      3. Alert (' Received message: ' + message)
      4. })
      5. Webviewjavascriptbridge.sendmessage (' Hello from the JavaScript, send message to OBJC ')
      6. }, False)
  5. The OBJECTC program sends messages to HTML.
    1. In the business of the program, use the following methods[CPP]View Plaincopy
        1. //Send message to HTML   
        2. [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 ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.