About JS calling OC

Source: Internet
Author: User

First: To import in the project

Javascriptcore.framework

In the WebView controller. H Reference

#import <JavaScriptCore/JavaScriptCore.h>

Then: we can

-(void) Webviewdidfinishload: (uiwebview *) WebView method

Pass

Jscontext *context = [WebView valueforkeypath:@ "DocumentView.webView.mainFrame.javaScriptContext"];

Gets a Jscontext object

This object I understand as JS's control center

Now there are two ways to make JS call our own method

1. Assign a block method to a context object directly:

context[@ "block"] = ^ (parameter) {};

JS can be called directly:

Block (parameter)

2. Assign the Self object of the WebView controller to a sub-class of the context object:

context[@ "Zhaozhi"] = self;

This way JS can be called through the object method, but to write a protocol in. h, the method is written in:

@protocol Webviewjsexport <jsexport>

-(void) Gotoexplosiondetail: (nsstring *) message;

@end

The controller complies with this Protocol

JS can call this method, the calling code:

Zhaozhi.method (* * *);

About JS calling OC

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.