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