Objective-c code invokes instances with JavaScript code _ios

Source: Internet
Author: User

Now, a good iOS app is sometimes associated with web content, and of course it also adds to the user experience, and it's better and more effective at showing the app's functionality.

Because I will use this part of the project in the function, make the record!

JS Call OC

Many applications inside more or less call the Web page, to achieve gorgeous effect, the so-called JS call oc ... For instance, there's a button on the Web page.

Click the button to jump the interface, the jump action by the OC Code implementation.

OC Call JS

Or for example, our OC code creates an input box, such as entering a user name, which is displayed on the Web page when it is finished, displaying the user's user name

I. Using WebView proxy method to realize the mutual invocation between OC and JS

Creating properties

@property (Nonatomic,strong) UIWebView * webview;

Comply with the WebView agreement 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 Invoke OC
-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype :(uiwebviewnavigationtype) navigationtype{
  nsstring * str = Request. url.relativestring;
  if ([str isequaltostring:@ "http://www.baidu.com/"]) {
    NSLog (@ "to find Niang");
  }
  Return YES
}
OC calls JS
-(void) Webviewdidfinishload: (UIWebView *) webview{
  nsstring * str = [Self.webview stringbyevaluatingjavascriptfromstring:@ "document.getElementById" ("" "index-kw" "\"). Value= "" "Li" "\" "];
  NSLog (@ "webviewdidfinishload=%@", str);
  
}

Two. Implementation using a third party framework (the name of the person who needs you and the HTML side to unify the method)

-(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];
  Setting can bridge [Webviewjavascriptbridge enablelogging];
  Set up bridging Self.bridge = [Webviewjavascriptbridge BridgeForWebView:self.webView];
  Set agent [Self.bridge setwebviewdelegate:self]; JS Call OC (Testobjccallback is the same as the HTML uniform method name) [Self.bridge registerhandler:@ "Testobjccallback" handler:^ (ID data,
    Wvjbresponsecallback responsecallback) {NSLog (@ "button clicked");
    self.view.backgroundcolor= [Uicolor Blackcolor];
  Responsecallback (@ "button clicked");
  
  
}];
  //oc calls JS-(void) Webviewdidfinishload: (UIWebView *) webview{NSLog (@ "Webviewdidfinishload");
[Self.bridge callhandler:@ "Registerhandler"];

 }

Thank you for reading, I hope to help you, thank you for your support for this site!

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.