This article includes JS call OC method and pass value, OC Call JS method and pass Value
Originally wanted to put HTML into the server, and then access, but think if the HTML in the local loading more helpful to understand, put HTML into the project
HTML code
<! DOCTYPE Html>"UTF-8">"margin-top:20px">"Button"Value="Invoke Local method (call)"onclick="Tianbai.call ()"></div><div><input type="Button"Value="evoke Getcall: (NSString *) callstring Pass value"onclick="Call ()"></div><script>var call=function () {var callinfo= Json.stringify ({"Jianshu":"Http://www.jianshu.com/users/55c8fdc3c6e7/latest_articles"}); Tianbai.getcall (callinfo);} var Callback=function (str) {alert (str);} var alercallback=function () {alert ('Success');}</script></body>The code for the above HTML: two buttons created
The first button binds the tianbai.call() method, and here tianbai is an object that is described in the following OC code to invoke the tianbai.call() method on behalf of the tianbai object call()
The second button binds the method that call() calls the method in the following JavaScript, which call() in JavaScript call() defines a callInfo parameter that represents the object in the method tianbai.getCall(callInfo) tianbai called
method and pass parameters callInfo , the following two methods are OC call JavaScript method, where callback returns str,alercallback for OC only call JavaScript Method!
OC CodeDemo using native JavaScriptCore class
Introduction of three nouns:
- Jscontext: Provides a context for JavaScript to run
- Bridges of Jsvalue:javascript and objective-c data and methods
- Jsexport: This is an agreement that, if you interact with a protocol, your own defined protocol must comply with this Protocol
Code in the ViewController.h
////ViewController.h//JavaScript////Created by Tianbai on 16/6/8.//Copyright 2016 Xiamen B branch network company. All rights reserved.//#import<UIKit/UIKit.h>#import<JavaScriptCore/JavaScriptCore.h>@protocolJsobjcdelegate <JSExport>
- (void) call;- (void) Getcall: (NSString *) callstring;@end
@interfaceViewcontroller:uiviewcontroller<uiwebviewdelegate, jsobjcdelegate>
@property (nonatomic, strong) Jscontext*Jscontext, @property (Strong, nonatomic) UIWebView*WebView;@end
Code in the VIEWCONTROLLER.M
////VIEWCONTROLLER.M//JavaScript////Created by Tianbai on 16/6/8.//Copyright 2016 Xiamen B branch network company. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; }
- (void) Viewdidappear: (BOOL) Animated {
Self.webview= [[UIWebView alloc]initwithframe:cgrectmake (0, -, [UIScreen mainscreen].bounds.size.width, [UIScreen Mainscreen].bounds.size.height]]; Self.webview.Delegate=Self ; NSString* Path = [[NSBundle mainbundle] Pathforresource:@"Index"OfType:@"HTML"]; Nsurl* URL =[Nsurl Fileurlwithpath:path]; Nsurlrequest* Request =[Nsurlrequest Requestwithurl:url]; [Self.webview Loadrequest:request]; [Self.view AddSubview:self.webView];}
- (void) Webviewdidfinishload: (UIWebView *) WebView {
Self.jscontext= [WebView Valueforkeypath:@"DocumentView.webView.mainFrame.javaScriptContext"]; self.jscontext[@"Tianbai"] =Self ; Self.jsContext.exceptionHandler= ^ (Jscontext *context, Jsvalue *Exceptionvalue) {
Context.exception=Exceptionvalue; NSLog (@"exception information:%@", Exceptionvalue); };}- (void) Call {
NSLog (@"Pager"); //then in the callback JS method callback the content to pass outJsvalue *callback = self.jscontext[@"Callback"]; //Pass the value to the Web side[Callback callwitharguments:@[@"Invoke local OC callback complete"]];}- (void) Getcall: (NSString *) callstring {
NSLog (@"get:%@", callstring); //the method of successful callback JS callbackJsvalue *callback = self.jscontext[@"Alercallback"]; [Callback Callwitharguments:nil]; //Add a prompt box directly//nsstring *str = @ "alert (' OC added JS hint succeeded ')";//[Self.jscontext evaluatescript:str];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@endIOS oc interaction with JS (JavaScriptCore implementation)