Mutual calls between JS Code and OC code, js Code oc

Source: Internet
Author: User

Mutual calls between JS Code and OC code, js Code oc

JS calls OC

Many applications call web pages more or less to achieve brilliant results. The so-called JS call OC... for example, there is a button on the web page.

Click the button to jump to the page. The jump is implemented by the OC code.

OC calls JS

For example, if the OC Code creates an input box, for example, enter the user name. After the input is complete, the user name is displayed on the webpage.

1. Use the webView proxy method to implement mutual calls between OC and JS

Create attributes

@ Property (nonatomic, strong) UIWebView * webView;

Comply with the webView protocol UIWebViewDelegate

-(Void) webViewLoad {NSURL * url = [[NSURL alloc] initWithString: @ "http://www.baidu.com"]; self. webView = [[UIWebView alloc] initWithFrame: self. view. bounds]; NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url]; self. webView. delegate = self; [self. webView loadRequest: request]; [self. view addSubview: self. webView];} // JS call OC-(BOOL) webView :( UIWebView *) webView shouldStartLoadWithRequest :( NSURLRequest *) request navigationType :( UIWebViewNavigationType) navigationType {NSString * str = request. URL. relativeString; if ([str isEqualToString: @ "http://www.baidu.com/"]) {NSLog (@ "");} return YES;} // OC call JS-(void) webViewDidFinishLoad :( UIWebView *) webView {NSString * str = [self. webView stringByEvaluatingJavaScriptFromString: @ "document. getElementById ("\" index-kw ""\"). value = "" \ "li" "\" "]; NSLog (@" webViewDidFinishLoad = % @ ", str );}

2. Use a third-party framework (you need to name the same method as the html user)

-(Void) webViewBridge {self. webView = [[UIWebView alloc] initWithFrame: [UIScreen mainScreen]. bounds]; NSString * path = [[NSBundle mainBundle] pathForResource: @ "ExampleApp.html" ofType: nil]; NSURL * url = [[NSURL alloc] initFileURLWithPath: path]; [self. webView loadRequest: [NSURLRequest requestWithURL: url]; self. webView. delegate = self; [self. view addSubview: self. webView]; // set [WebViewJavascriptBridge enableLogging]; // set self for bridging. bridge = [WebViewJavascriptBridge bridgeForWebView: self. webView]; // set proxy [self. bridge setWebViewDelegate: self]; // JS call OC (testObjcCallback is the same method name as html) [self. bridge registerHandler: @ "testObjcCallback" handler: ^ (id data, WVJBResponseCallback responseCallback) {NSLog (@ "click"); self. view. backgroundColor = [UIColor blackColor]; responseCallback (@ "click") ;}] ;}// OC call JS-(void) webViewDidFinishLoad :( UIWebView *) webView {NSLog (@ "webViewDidFinishLoad"); [self. bridge callHandler: @ "registerHandler"];}

 

Related Article

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.