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.