The use of IOS JS interaction system Jscontext Implementation JS Call OC method

Source: Internet
Author: User

iOS JS interaction is divided into two blocks: 1.oc call JS

This piece is relatively simple to implement.

My project is loaded with local html,js,css, and it is important to note that when you drag the files into the project, you choose to copy to the project, (the folder you dragged is blue, relative to the path), or the path to the CSS,JS is problematic.

Load Local HTML:

OC Call JS: a code to fix

2.js call OC JS call OC is divided into two kinds: 1.js is the direct call method

Here's a new API JavaScriptCore introduced by IOS7, first we introduce this class and initialize a Jscontext object

In the Webviewdidfinishload method.

This is obtained through the current WebView key to Jscontext

Connect our context object with the JS method to establish a bridging connection,

2.js end-of-object invocation method

When it comes to object tuning, we must first have an object, so we first need to create a JS interaction class, and declare and implement the corresponding interactive method, here we need to note that we need to import the JavaScriptCore framework in this custom interaction class, Declare a protocol that complies with the Jsexport protocol (a bit around, look at the picture), declare our interaction method in this protocol, and finally let our interaction class conform to our custom protocol

Implement the corresponding JS interaction method in the. m of the interaction class, and implement the action we want

Notice that on the diagram

Jsexportas(Invoke,-(void) Invoke: (nsstring *) method args: (nsstring *) args);

This method does not, the function of this method is when the JS side of the method has two or more than two parameters, we need to let-(void) Invoke: (nsstring *) method args: (nsstring *) args; This method replaces the corresponding JS method, because the OC side of the method name must be consistent with the JS side, otherwise it will not be called

Finally, we initialize our interaction object in our Webviewdidfinishload method and let the context connect with it, OK, done

Hybridapi calls this method--

3. Objective-c calls the JavaScript method code as follows:

[self. WebView evaluatejavascript: [nsstring stringwithformat:@ "__yutaapponprepare ()" ]

Completionhandler: ^ (ID response, nserror *error)

{

}];

The use of IOS JS interaction system Jscontext Implementation JS Call OC method

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.