IOS JS and native communicate with each other

Source: Internet
Author: User

The mechanism of mutual communication between JS and navive js–> native

At present, as of Ios7,ios native does not provide JS direct call native way, only through UIWebView related Uiwebviewdelegate protocol

- (bool) (uiwebview *) webView Span class= "NF" >shouldstartloadwithrequest: (nsurlrequest *) request navigationtype: ( Uiwebviewnavigationtype) navigationtype

< span class= "n" > method to intercept, and in this method, depending on the URL of the protocol or feature string to do the call method or trigger the event, such as

/* * The return value of the method is a bool value. * Return yes: to let the browser perform the default action, such as a link jump * Return No: The default action is not to perform the browser, here because the URL protocol to judge JS to perform native operation, is certainly not the browser default operation, so return no*/-(BOOL) WebView :(UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: (Uiwebviewnavigationtype) Navigationtype {    Nsurl *url = [request URL];    if ([[URL scheme] isequaltostring:@ "CallFunction") {        //Call the native method        return NO;    } else if ([[URL scheme] isequaltostring:@ "Sendevent") {        //Trigger event        return NO;    } else {        return YES;}    }

Although in this way, JS call native is 异步 , but the efficiency is very high, I pass in the JS call, the time passed into the navive and then subtract the way to calculate, average only 5ms interval.

How to trigger this method to intercept

The simplest and most practical way is to use JS to create a hidden iframe setting src, code:

function js2native () {      = document.createelement ("iframe");      IFRAME.SRC="callfunction://";       ' None ' ;      Document.body.appendChild (IFRAME);      Iframe.parentNode.removeChild (iFrame);       NULL ;}

By looking at the Iosexec method of PhoneGap source code, there are also ways to use XMLHttpRequest or modify hash to trigger method interception, but it is not recommended because there are bugs or other reasons.

Native–> JS

Native Call JS is very simple and convenient, only need

  [webView stringbyevaluatingjavascriptfromstring:@ "alert (' Hello world! ')" ];

And the method is 同步 .

Debugging

Although it is now possible to directly view the WebView page in the simulator directly with Safari's development mode, but after trying, the most wanted is also the most important JS debugging, or not support, can not be debugging JS breakpoint, or rely on the console to get ... Of course, CSS style debugging support is good.

2014-04-09 update: Yesterday found that Safari is capable of debugging the WebView page in the simulator with JS Breakpoint, before it was because I did not set ... 启用所有断点

After the simulator enters the WebView page, you can open safari and then select the page under the development->iphone Simulator menu and go to Debug.

Related information

Tang Qi-a summary of UIWebView and PhoneGap

The Webviewjavascriptbridge project on GitHub

The famous PhoneGap

IOS JS and native communicate with each other

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.