网页 js
2015-07-21 09:51
330 查看
JS与iOS之间的通信,主要运用两个方法:(PhoneGap框架也是基于此原理)
1、UIWebView的 stringByEvaluatingJavaScriptFromString方法
2、UIWebViewDelegate的
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType方法
示例:
上部分是一个UIWebView,实现UIWebViewDelegate
![](http://hi.csdn.net/attachment/201201/13/0_132642602390fx.gif)
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"jm/info" ofType:@"html"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
[self.webView loadRequest:request];
}
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if([request.mainDocumentURL.relativePath isEqualToString:@"/getInfo/name"])
{
NSString *info = [[UIDevice currentDevice] name];
NSString *js = [NSString stringWithFormat:@"showInfo(\"name\",\"%@\")",info];
[self.webView stringByEvaluatingJavaScriptFromString:js];
return false;
}
if([request.mainDocumentURL.relativePath isEqualToString:@"/getInfo/systemVersion"])
{
NSString *info = [[UIDevice currentDevice] systemVersion];
NSString *js = [NSString stringWithFormat:@"showInfo(\"systemVersion\",\"%@\")",info];
[self.webView stringByEvaluatingJavaScriptFromString:js];
return false;
}
return true;
}
JS代码:
<!DOCTYPE html>
<html>
<head>
<title>city</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="jquery.mobile-1.0.css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.mobile-1.0.js"></script>
<script>
function getInfo(name)
{
window.location = "/getInfo/"+name;
}
function showInfo(id,info)
{
$("p#"+id).html(info);
}
</script>
</head>
<body>
<div data-role="page">
<div data-role="content">
<h2>Divice Info</h2>
<div data-role="collapsible-set" data-theme="c" data-content-theme="d">
<div data-role="collapsible">
<h3 onclick="getInfo('name')">name</h3>
<p id="name"></p>
</div>
<div data-role="collapsible">
<h3 onclick="getInfo('systemVersion')">systemVersion</h3>
<p id="systemVersion"></p>
</div>
</div>
</div>
</div>
</body>
</html>
1、UIWebView的 stringByEvaluatingJavaScriptFromString方法
2、UIWebViewDelegate的
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType方法
示例:
上部分是一个UIWebView,实现UIWebViewDelegate
![](http://hi.csdn.net/attachment/201201/13/0_132642602390fx.gif)
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"jm/info" ofType:@"html"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
[self.webView loadRequest:request];
}
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if([request.mainDocumentURL.relativePath isEqualToString:@"/getInfo/name"])
{
NSString *info = [[UIDevice currentDevice] name];
NSString *js = [NSString stringWithFormat:@"showInfo(\"name\",\"%@\")",info];
[self.webView stringByEvaluatingJavaScriptFromString:js];
return false;
}
if([request.mainDocumentURL.relativePath isEqualToString:@"/getInfo/systemVersion"])
{
NSString *info = [[UIDevice currentDevice] systemVersion];
NSString *js = [NSString stringWithFormat:@"showInfo(\"systemVersion\",\"%@\")",info];
[self.webView stringByEvaluatingJavaScriptFromString:js];
return false;
}
return true;
}
JS代码:
<!DOCTYPE html>
<html>
<head>
<title>city</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="jquery.mobile-1.0.css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.mobile-1.0.js"></script>
<script>
function getInfo(name)
{
window.location = "/getInfo/"+name;
}
function showInfo(id,info)
{
$("p#"+id).html(info);
}
</script>
</head>
<body>
<div data-role="page">
<div data-role="content">
<h2>Divice Info</h2>
<div data-role="collapsible-set" data-theme="c" data-content-theme="d">
<div data-role="collapsible">
<h3 onclick="getInfo('name')">name</h3>
<p id="name"></p>
</div>
<div data-role="collapsible">
<h3 onclick="getInfo('systemVersion')">systemVersion</h3>
<p id="systemVersion"></p>
</div>
</div>
</div>
</div>
</body>
</html>
相关文章推荐
- 【JS进阶3】闭包和匿名函数
- js获取页面及个元素高度、宽度
- 鼠标划过表格行变色效果JS
- js捕获键盘事件之keydown、keyup以及keypress
- JS获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
- 异步安全加载javascript文件的方法
- JSP+SSH实现默认全部导出或可选部分数据导出到Excel
- JavaScript经典代码总结
- javascript获取文件后缀名
- js 判断滚动条的滚动方向
- javascript 文字大小自动适应文本框 (文字大小自动调整)
- ideaIU14.4配置coco 4000 s2d-js环境
- JSON概述
- JavaScript高级——事件监听控制
- ------------参数传递的四种形式----- URL,超链接,js,form表单
- Day03-JavaScript高级编程
- Day02-CSS+Javascript基础
- JavaScript——高级——DOM——节点访问
- 异步安全加载javascript文件的方法
- Javascript实现获取及设置光标位置的方法