IOS development uses JavaScript to invoke the OC method rather than Url_ios

Source: Internet
Author: User

First look at how to click a button on the project WebView, you can reach the calling OC code

The above page is nested inside the webview of a project page, print order after clicking (Click event is a JS method), need to call the OC inside the integrated Bluetooth printer function, to complete the printing.

So here you can only use the JS code to invoke the OC code directly .

1. First create an iOS class, because here generally need to do a set of Andro, iOS end to do a set, so generally named as a difference

. h
#import <Foundation/Foundation.h>
#import <JavaScriptCore/JavaScriptCore.h>

/**
 * JS invoke the code of main OC, need to use this agreement to do
 * * *
@protocol jsobjectprotocol <JSExport>

@required
#pragma Mark-js invokes the OC method and prints the jsonstring out
-(void) Print: (NSString *) jsonstring;

@end


@interface ios:nsobject<jsobjectprotocol>



@end
. m

@implementation IOS


#pragma mark-print jsonstring
-(void) Print: (NSString *) jsonstring{

  // Printed Specific Events
}

2. Call registered iOS registration after the end of the page loading

#pragma mark-the Web page is loaded, the iOS object is registered, and the printer is automatically checked for automatic connection
-(void) Webviewdidfinishload: (UIWebView *) webview{

  // First create the Jscontext object (where the current WebView key gets to the Jscontext)
  //The object must be created, otherwise it will not be able to invoke the method in the protocol
  iOS *ios = [[IOS alloc] init];

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

  context[@ "ios"] = iOS;

}

3. In the JS Click event similar to the following call can be (the following code written in the corresponding JS code inside, rather than your OC code inside)

 if (window["Android"] && window["Android" ["Jsprint"]) {
  var andprint = window. Android.jsprint (Json.stringify (d));
 } else if (window["ios"] && window["ios" ["print"]) {

   window["ios"] ["print"] (json.stringify (d));

} else {
   This.alert.autoCloseTip ("Print method not Found!", 1000);
 }

This is all about how iOS uses JavaScript code to invoke the entire contents of the OC method, and hopefully it will help you learn about iOS development. I hope you will support the cloud-dwelling community.

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.