iOS中UIWebView与其中网页的javascript的交互
swift代码:
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var theWebView: UIWebView!
override func viewDidLoad() {
//加载本地html
let res = NSBundle.mainBundle().pathForResource("index",ofType:"html")
let url = NSURL(fileURLWithPath: res!);
let request = NSURLRequest(URL: url);
self.theWebView.loadRequest(request);
self.theWebView.delegate = self;
}
//让js可以调用swift
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
//判断是不是js在调用swift,如果是,则处理并返回false
if(request.URL!.scheme == "myschema"){
let host = request.URL!.host;
if(host == "go"){
let query = request.URL!.query!;
print(query);
let split = query.componentsSeparatedByString("=");
let text = split[1];
self.theWebView.stringByEvaluatingJavaScriptFromString("changeContent(\"" + text + "\")");
}
return false;
}else{
return true;
}
}
//swift调用js
@IBAction func btnClicked(sender: AnyObject) {
self.theWebView.stringByEvaluatingJavaScriptFromString("changeContent(\"123\")");
}
}
网页代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<span id="theSpan"></span>
<button onclick="doIt()">js调用swift</button>
<input type="text" id="t">
<script>
//让swift可以调用js
function changeContent(str){
document.getElementById('theSpan').innerHTML = str;
}
//js调用swift
function doIt(){
document.location.href = "myschema://go?a=" + document.getElementById("t").value;
}
</script>
</body>
</html>
iOS中UIWebView与其中网页的javascript的交互的更多相关文章
- iOS中UIWebView执行JS代码(UIWebView)
iOS中UIWebView执行JS代码(UIWebView) 有时候iOS开发过程中使用 UIWebView 经常需要加载网页,但是网页中有很多明显的标记让人一眼就能看出来是加载的网页,而我们又不想被 ...
- iOS中UIWebView使用JS交互
iOS中偶尔也会用到webview来显示一些内容,比如新闻,或者一段介绍.但是用的不多,现在来教大家怎么使用js跟webview进行交互. 这里就拿点击图片获取图片路径为例: 1.测试页面html & ...
- iOS中UIWebView使用JS交互 - 机智的新手
iOS中偶尔也会用到webview来显示一些内容,比如新闻,或者一段介绍.但是用的不多,现在来教大家怎么使用js跟webview进行交互. 这里就拿点击图片获取图片路径为例: 1.测试页面html & ...
- iOS中UIWebview中网页宽度自适应的问题
有的网页中会使用"<meta name="viewport" content="width=device-width, initial-scale=1.0 ...
- IOS中UIWebView执行javaScript脚本时注意点
1.webView之所以能够滚动,因为它内部有一个UIScrollView子控件 2.移除webView顶部和底部灰色的一层view * 遍历webView中scrollView内部的所有子控件 * ...
- iOS 使用UIWebView把oc代码和javascript相关联
首先请参看一篇文章,作者写的很明白,请参看原地址 http://blog.163.com/m_note/blog/static/208197045201293015844274/. 其实,oc和js的 ...
- iOS中 UIWebView加载网络数据 技术分享
直奔核心: #import "TechnologyDetailViewController.h" #define kScreenWidth [UIScreen mainScreen ...
- iOS 中UIWebView的cookie
有关cookie是什么,大家可以自行百度,本文我获得cookie的目的是得到一个userID. 下面的是代码. - (void)getUserIDFromCookie { NSHTTPCookieSt ...
- ios中UIWebview中加载本地文件
[super viewDidLoad]; webview=[[UIWebView alloc] initWithFrame:self.view.bounds]; [self.view addSubvi ...
随机推荐
- hdu1251 字典树or map
一道字典树的题,不过看起来用map更为简单 传送门 题意: 给出一堆字符串构成一个字典,求字典里以某字符串为前缀的字符串有几个 思路: 输入字符串时把字符串的前缀全部存进map并标记次数 查询时直接输 ...
- 收集的github的东西
1.voghDev/PdfViewPager-Android widget that can render PDF documents stored on SD card, linked as ass ...
- Eclipse中添加PyDev插件
思路 1.启动Eclipse, 2.点击Help->Install New Software... 3.在弹出的对话框中,点Add 按钮. 4.Name中填:Pydev, Location中填h ...
- HDU1176
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 常用js或jq效果汇总
实时监控输入框改变 $('#password').bind('input propertychange', function() {}
- 20145334赵文豪 《Java程序设计》第4周学习总结
20145334赵文豪 <Java程序设计>第4周学习总结 教材学习内容总结 第六章知识点总结 1-继承共同行为:如果在程序设计上存在着重复,那就需要修改,可以吧相同的程序代码提升(pul ...
- 让你的网站秒开 为IIS启用“内容过期”
让你的网站秒开,为IIS启用“内容过期” 什么是内容过期? 当用户第一次访问你的网站,浏览器从你的网站主机下载内容,如果用户第二次访问你的网站,浏览器从缓存读取内容.你知道浏览器从缓存读取网页有多快吗 ...
- C# 特殊关键字
1.static 表示被修饰的对象的生命周期与当前的应用程序域相同,因此可以在多个实例中共享 2.const 表示常量且只能在声明时赋值,因此const的常量在编译时就能确定,它如果没有被static ...
- A trip through the Graphics Pipeline 2011_07_Z/Stencil processing, 3 different ways
In this installment, I’ll be talking about the (early) Z pipeline and how it interacts with rasteriz ...
- java跳出外部循环
java跳出外部循环两种方法 //用flag标识 boolean flag = true; for(int i=0;i<10&&flag;i++){ for(int j=0;j& ...