iOS Cordova native and JS communication principle

Source: Internet
Author: User

In iOS, Cordova's Cdvviewcontroller inherits Viewcontroller, and each H5 page corresponds to a cdvviewcontroller,

There is a webview, which is a view that is specifically displayed on the H5 page in iOS.

H5 and the original end of the interaction, mainly through the form of plug-ins, the original side write a class inherit Cdvplugin, and then implement the method, JS over the way to encapsulate each interface,

The call is finally made by calling exec in Cordova.js, passing in the service class name, method name, and callback functions and parameters.

Its principle of communication is: there are two ways

1. By inserting an invisible iframe into HTML through the Iframe,cordova.exec function, a special URL is loaded to the UIWebView request.

The URL includes the class name of the plugin plug-in, the method name.

The Uiweibviewdegate shouldstartloadwithrequest is triggered because the URL is requested

This will enter the native, and the native is processed by the request to get the class name and method name

2, through the xhr,cordova.exec through the initiation of a XHR request, this time will be Nsurlprotocol intercept

The native caninitwithrequest will be called to enter the native

Native Invoke JS method, is directly through the stringbyevaluatingjavascriptfromstring directly call JS method

iOS Cordova native and JS communication principle

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.