iOS开发,UIWebview与H5之间的交互

  1. 将HTML页面加载到UIWebview控件中:

A:本地加载:

NSString *webPath = [[NSBundle  mainBundle]pathForResource:@“HelloWord" ofType:@"html”];//获取文件路径
NSURL *webURL = [NSURL  fileURLWithPath:webPath];//通过文件路径字符串设置URL

//NSURL *url = [[NSURL alloc] initFileURLWithPath:webPath];


NSURLRequest *URLRequest = [NSURLRequest  requestWithURL:webURL];//设置请求提交的相关URL
[self.webView loadRequest:URLRequest];//提交请求

B:从服务器加载HTML页面

NSString *urlStr = [NSString stringWithFormat:@"%@/appservice/getAnnouncementDetail.do?id=%@",O2O_URL,self.idStr];
NSURL *url = [NSURL URLWithString:urlStr];
NSLog(@"url:%@",url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

2.加入UIWebViewDelegate委托(如果想要进行相应的数据交互必须添加相应的我委托)

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType//html页面的js动作触发时运行

-(void)webViewDidFinishLoad:(UIWebView *)webView //html加载完后触发

3.OC提交JS请求.运行JS方法

[self.webView stringByEvaluatingJavaScriptFromString:@”方法名(%@)",参数]];

4.JS提交请求到OC

-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{
NSString *requestString = [[request  URL]absoluteString];//获取请求的绝对路径.
NSArray *components = [request  StringcomponentsSeparatedByString:@":”];//提交请求时候分割参数的分隔符
if ([components  count] >1 && [(NSString *)[components  objectAtIndex:0]isEqualToString:@"testapp"]) {
//过滤请求是否是我们需要的.不需要的请求不进入条件
if([(NSString *)[components objectAtIndex:1]isEqualToString:@"change"])
{
//所要执行的操作
}
returnNO;
}
returnYES;
}

5.在HTML5中JS的写法

1.接收请求的方法写法

function
fund(){
//操作
}

2.提交请求的方法写法

function sendCommand(cmd,param){
var url="testapp:"+cmd+":"+param;//组合请求字符串
document.location = url;//提交的请求
}

最后附上例子程序(传送阵):

http://download.csdn.net/detail/fqcl992/6929375

id="iframeu2254328_2" src="http://pos.baidu.com/dckm?sz=336x280&rdid=2254328&dc=2&di=u2254328&dri=2&dis=0&dai=5&ps=1381x195&coa=at%3D3%26rsi0%3D336%26rsi1%3D280%26pat%3D17%26tn%3DbaiduCustNativeAD%26rss1%3D%2523FFFFFF%26conBW%3D1%26adp%3D1%26ptt%3D0%26titFF%3D%26titFS%3D14%26rss2%3D%2523000000%26titSU%3D0&dcb=BAIDU_SSP_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1464245760362&ti=iOS%E5%BC%80%E5%8F%91%EF%BC%8CUIWebview%E4%B8%8EH5%E4%B9%8B%E9%97%B4%E7%9A%84%E4%BA%A4%E4%BA%92%20-%20%E7%BD%91%E7%BB%9C%E6%8A%80%E6%9C%AF%E7%BC%96%E7%A8%8B&ari=1&dbv=0&drs=1&pcs=726x652&pss=726x1382&cfv=0&cpl=6&chi=6&cce=true&cec=UTF-8&tlm=1464216960&ltu=http%3A%2F%2Fwww.unjeep.com%2Fq%2F841949662.htm&ltr=http%3A%2F%2Fwww.unjeep.com%2Ftag%2Fh5-ios%2F&ecd=1&psr=1280x800&par=1280x722&pis=-1x-1&ccd=24&cja=true&cmi=65&col=zh-cn&cdo=-1&tcn=1464245760&qn=fa001a82bd444c0f&tt=1464245760354.89.197.197" width="336" height="280" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="box-sizing: border-box; border-width: 0px; vertical-align: bottom; margin: 0px;">
id="iframeu2254328_3" src="http://pos.baidu.com/dckm?sz=336x280&rdid=2254328&dc=2&di=u2254328&dri=3&dis=0&dai=6&ps=1382x195&coa=at%3D3%26rsi0%3D336%26rsi1%3D280%26pat%3D17%26tn%3DbaiduCustNativeAD%26rss1%3D%2523FFFFFF%26conBW%3D1%26adp%3D1%26ptt%3D0%26titFF%3D%26titFS%3D14%26rss2%3D%2523000000%26titSU%3D0&dcb=BAIDU_SSP_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1464245760362&ti=iOS%E5%BC%80%E5%8F%91%EF%BC%8CUIWebview%E4%B8%8EH5%E4%B9%8B%E9%97%B4%E7%9A%84%E4%BA%A4%E4%BA%92%20-%20%E7%BD%91%E7%BB%9C%E6%8A%80%E6%9C%AF%E7%BC%96%E7%A8%8B&ari=1&dbv=0&drs=1&pcs=726x652&pss=726x1383&cfv=0&cpl=6&chi=6&cce=true&cec=UTF-8&tlm=1464216960&ltu=http%3A%2F%2Fwww.unjeep.com%2Fq%2F841949662.htm&ltr=http%3A%2F%2Fwww.unjeep.com%2Ftag%2Fh5-ios%2F&ecd=1&psr=1280x800&par=1280x722&pis=-1x-1&ccd=24&cja=true&cmi=65&col=zh-cn&cdo=-1&tcn=1464245760&qn=d34743dd632912c1&tt=1464245760354.96.200.201" width="336" height="280" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="box-sizing: border-box; border-width: 0px; vertical-align: bottom; margin: 0px;">
更多相关文章
  • 感谢大家的关注,也给我一份动力,让我继续前进.有了自己的家庭有了孩子,过着上有老下有小的生活,能够挤出点时间学习真的很难,每天弄好孩子睡觉已经是晚上10点左右了,然后再弄自己的事情,一转眼很快就到12点了,就要去睡了,现在身体汤不牢啊,如果不早点睡,第二天上班
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值