OC and JS call each other and OCJS call
Html5 is used in recent projects, involving OC calling JS and JS calling OC methods. Here we will introduce the problems and implementation methods.
/// ViewController. h // OC_And_JS /// Created by Zhang Jie on 15/7/9. // Copyright©2015 Zhang Jie. all rights reserved. // # import <UIKit/UIKit. h> @ interface ViewController: UIViewController <UIWebViewDelegate> @ property (weak, nonatomic) IBOutlet UIButton * handle; @ property (weak, nonatomic) IBOutlet UIButton * handle; @ property (weak, nonatomic) IBOutlet UIWebView * mWebView; @ property (weak, nonatomic) IBOutlet UILabel * handle;-(IBAction) ocCallJsNoParams :( id) sender;-(IBAction) Consumer :( id) sender; @ end
/// ViewController. m // OC_And_JS /// Created by Zhang Jie on 15/7/9. // Copyright©2015 Zhang Jie. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; _ mWebView. delegate = self; // open the URL NSString * path = [[NSBundle mainBundle] pathForResource: @ "index" ofType: @ "html"]; [self. mWebView loadRequest: [NSURLRequest requestWithURL: [NSURL fileURLWithPath: path];}-(BOOL) webView :( UIWebView *) web View shouldStartLoadWithRequest :( NSURLRequest *) request navigationType :( UIWebViewNavigationType) navigationType {NSString * urlstr = request. URL. absoluteString; nsange range = [urlstr rangeOfString: @ "ios: // ~zhangjie"]; if (range. length! = 0) {_ js_call_oc_show.text = [NSString stringWithFormat: @ "Visit URL: % @", urlstr];} return YES;}-(void) webView :( nonnull UIWebView *) webView didFailLoadWithError :( nullable NSError *) error {NSLog (@ "loading failed");}-(void) webViewDidStartLoad :( nonnull UIWebView *) webView {NSLog (@ "Start loading");}-(void) webViewDidFinishLoad :( nonnull UIWebView *) webView {NSLog (@ "start to end "); // It is best to call this method in js or later}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} -(IBAction) ocCallJsNoParams :( id) sender {NSString * js = [NSString stringWithFormat: @ "ocCallJsNoParamsFunction ();"]; [self. mWebView callback: js];}-(IBAction) ocCallJsHasParams :( id) sender {NSString * js = [NSString stringWithFormat: @ "ocCallJsHasParamsFunction ('% @', '% @'); ", @" Your Zhangjie ", @" http://jwzhangjie.cn "]; [self. mWebView stringByEvaluatingJavaScriptFromString: js] ;}@ end
Function ocCallJsNoParamsFunction () {alert ("No Parameter Method in JS call OC"); var e = document. getElementById ("js_shouw_text"); e. options. add (new Option ("No Parameter Method in JS call OC", 2);} function ocCallJsHasParamsFunction (name, url) {alert (name + ") blog address: "+ url); var e = document. getElementById ("js_shouw_text"); e. options. add (new Option ("OC calls the method with parameters in JS", 2 ));}
<! DOCTYPE html>
Avoidance 1: It is best for OC to call JS content in the webViewDidFinishLoad method or later
Avoid 2: do not include a path in src When referencing js or css in html, because the files after installation are all under the same directory
Avoidance 3: OC calling JS specifications
NSString *js = [NSString stringWithFormat:@"ocCallJsHasParamsFunction('%@','%@');",@"jwzhangjie",@"http://jwzhangjie.cn"]; [self.mWebView stringByEvaluatingJavaScriptFromString:js];
Avoidance 4: JS calls OC. Here, a request is sent through html, and then shouldStartLoadWithRequest is used in ios to intercept the request and process it according to the request url.
Function js_call_oc () {var iFrame; iFrame = document. createElement ("iframe"); iFrame. setAttribute ("src", "ios: // jwzhangjie"); iFrame. setAttribute ("style", "display: none;"); iFrame. setAttribute ("height", "0px"); iFrame. setAttribute ("width", "0px"); iFrame. setAttribute ("frameborder", "0"); document. body. appendChild (iFrame); // This iFrame is useless after a request is initiated, so remove it from the dom. parentNode. removeChild (iFrame); iFrame = null ;}
-(BOOL) webView :( UIWebView *) webView shouldStartLoadWithRequest :( NSURLRequest *) request navigationType :( UIWebViewNavigationType) navigationType {NSString * urlstr = request. URL. absoluteString; nsange range = [urlstr rangeOfString: @ "ios: // ~zhangjie"]; if (range. length! = 0) {_ js_call_oc_show.text = [NSString stringWithFormat: @ "please visit: % @", urlstr];} return YES ;}
Source Code address:
Http://jwzhangjie.cn/forum.php? Mod = viewthread & tid = 3 & page = 1 & extra = # pid3
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.