最近项目中要用到html5来实现,涉及到OC调用JS,以及JS调用OC的方法,这里把遇到的问题以及实现方法介绍一下。



//

//ViewController.h

//OC_And_JS

//

//Createdby张杰on15/7/9.

//Copyright2015年张杰.Allrightsreserved.

//

#import<UIKit/UIKit.h>

@interfaceViewController:UIViewController<UIWebViewDelegate>

@property(weak,nonatomic)IBOutletUIButton*oc_call_js_no_params;

@property(weak,nonatomic)IBOutletUIButton*oc_call_js_has_params;

@property(weak,nonatomic)IBOutletUIWebView*mWebView;

@property(weak,nonatomic)IBOutletUILabel*js_call_oc_show;

-(IBAction)ocCallJsNoParams:(id)sender;

-(IBAction)ocCallJsHasParams:(id)sender;

@end



[objc]view plaincopy

//

//ViewController.m

//OC_And_JS

//

//Createdby张杰on15/7/9.

//Copyright2015年张杰.Allrightsreserved.

//

#import"ViewController.h"

@interfaceViewController()

@end

@implementationViewController

-(void)viewDidLoad{

[superviewDidLoad];

_mWebView.delegate=self;

//打开URL

NSString*path=[[NSBundlemainBundle]pathForResource:@"index"ofType:@"html"];

[self.mWebViewloadRequest:[NSURLRequestrequestWithURL:[NSURLfileURLWithPath:path]]];

}

-(BOOL)webView:(UIWebView*)webViewshouldStartLoadWithRequest:(NSURLRequest*)requestnavigationType:(UIWebViewNavigationType)navigationType{

NSString*urlstr=request.URL.absoluteString;

NSRangerange=[urlstrrangeOfString:@"ios://jwzhangjie"];

if(range.length!=0){

_js_call_oc_show.text=[NSStringstringWithFormat:@"请访问地址:%@",urlstr];

}

returnYES;

}

-(void)webView:(nonnullUIWebView*)webViewdidFailLoadWithError:(nullableNSError*)error{

NSLog(@"加载失败");

}

-(void)webViewDidStartLoad:(nonnullUIWebView*)webView{

NSLog(@"开始加载");

}

-(void)webViewDidFinishLoad:(nonnullUIWebView*)webView{

NSLog(@"开始结束");

//对于调用js的时候最好这个方法里面或者之后

}

-(void)didReceiveMemoryWarning{

[superdidReceiveMemoryWarning];

//Disposeofanyresourcesthatcanberecreated.

}

-(IBAction)ocCallJsNoParams:(id)sender{

NSString*js=[NSStringstringWithFormat:@"ocCallJsNoParamsFunction();"];

[self.mWebViewstringByEvaluatingJavaScriptFromString:js];

}

-(IBAction)ocCallJsHasParams:(id)sender{

NSString*js=[NSStringstringWithFormat:@"ocCallJsHasParamsFunction('%@','%@');",@"jwzhangjie",@"http://jwzhangjie.cn"];

[self.mWebViewstringByEvaluatingJavaScriptFromString:js];

}

@end


[javascript]view plaincopy

functionocCallJsNoParamsFunction()

{

alert("OC调用JS中的无参方法");

vare=document.getElementById("js_shouw_text");

e.options.add(newOption("OC调用JS中的无参方法",2));

}

functionocCallJsHasParamsFunction(name,url)

{

alert(name+"的博客地址为:"+url);

vare=document.getElementById("js_shouw_text");

e.options.add(newOption("OC调用JS中的有参方法",2));

}


[html]view plaincopy

<!DOCTYPEhtml>

<htmllang="zh-CN">

<head>

<metacharset="utf-8">

<title>OC与JS互相调用</title>

</head>

<body>

<div>

<selectid="js_shouw_text">

<option>

展示OC调用JS无参数

</option>

</select>

</div>

<div>

<BR/>

<inputtype="button"value="JS调用OC方法"onclick="js_call_oc()"/>

</div>

<!--这里要清楚,虽然test.js跟index.html不同及目录,实际安装到程序里面后,是在同级目录的,所以这里src不能加目录,同样css也是一样的-->

<scripttype="text/javascript"src="test.js"charset="UTF-8"></script>

<scripttype="text/javascript">

functionjs_call_oc()

{

variFrame;

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);

//发起请求后这个iFrame就没用了,所以把它从dom上移除掉

iFrame.parentNode.removeChild(iFrame);

iFrame=null;

}

</script>

</body>

</html>


规避1:对于OC去调用JS内容最好在webViewDidFinishLoad方法里或者之后

规避2:在html里面引用js或者css的时候src不要带有路径,因为安装后文件都在同级目录下面

规避3:OC调用JS的规范

[objc]view plaincopy

NSString*js=[NSStringstringWithFormat:@"ocCallJsHasParamsFunction('%@','%@');",@"jwzhangjie",@"http://jwzhangjie.cn"];

[self.mWebViewstringByEvaluatingJavaScriptFromString:js];

规避4:JS调用OC,这里通过html里面发送一个请求,然后在ios中使用shouldStartLoadWithRequest拦截请求,根据请求url的不同进行处理。


[javascript]view plaincopy

functionjs_call_oc()

{

variFrame;

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);

//发起请求后这个iFrame就没用了,所以把它从dom上移除掉

iFrame.parentNode.removeChild(iFrame);

iFrame=null;

}


[objc]view plaincopy

-(BOOL)webView:(UIWebView*)webViewshouldStartLoadWithRequest:(NSURLRequest*)requestnavigationType:(UIWebViewNavigationType)navigationType{

NSString*urlstr=request.URL.absoluteString;

NSRangerange=[urlstrrangeOfString:@"ios://jwzhangjie"];

if(range.length!=0){

_js_call_oc_show.text=[NSStringstringWithFormat:@"请访问地址:%@",urlstr];

}

returnYES;

}