iOS webView和JS简单交互处理
2015-05-12 17:03
375 查看
首先
url = @"http://42.96.155.42:8080/crm/loginRelationServlet?openId=A786D29EBAD81123313619A2F19B9447&accessToken=8F796D79CE14E4C5A7AC194D8135E2BB&nickname=Mayer";
页面源文件:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>qq_user</title>
</head>
<body>
<a id="qq_userid">1000049</a>
<a id="qq_password">123456</a>
</body>
</html>
现在的目的是取出账号(qq_userid所对应的值)和密码(qq_password所对应的值)。
iOS客户端中倘若获取到网页上的元素的最简单的两个方法:
1.在
shouldStartLoadWithRequest方法中拿到request的url,从而获取url连接后面所附带的参数。
2.利用webView的强大方法
stringByEvaluatingJavaScriptFromString
来获取网页上的具体元素。(而此种方法需要少许知道js语句)
在此,讨论第二种方法。
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *String =
@"UIWebView是iOS最常用的SDK之一,它有一个stringByEvaluatingJavaScriptFromString方法可以将javascript嵌入页面中,通过这个方法我们可以在iOS中与UIWebView中的网页元素交互";
NSString *user =
@"document.getElementById('qq_userid').innerHTML";
NSString *pwd =
@"document.getElementById('qq_password').innerHTML";
NSString *str = [NSString
stringWithFormat:@"document.getElementById('qq_userid').innerHTML ='%@';",String];
NSString *userResult = [self
stringByEvaluatingJavaScriptFromString:user];
NSString *pwdResult = [self
stringByEvaluatingJavaScriptFromString:pwd];
}
至此,可以拿到账号和密码。
警告1 stringByEvaluatingJavaScriptFromString:此方法的使用最好在webViewDidFinishLoad方法中调用
警告2 要想拿某个页面上的元素,必须首先保证此页面加载。(即在
shouldStartLoadWithRequest
方法中必须放回yes)
其实警告1也是基于警告2.
其方法的难点在于js语句的使用。
下面简单的分析下js语句:
getElementById('qq_password') ==通过id获取页面元素。
innerHTML属性 ==获取html当前标签的起始和结束里面的内容
document.getElementById('qq_userid').innerHTML ='%@'; ==会修改所获取到的值。
插入js 并执行js函数
NSString *insertString = [NSString stringWithFormat:
@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function jsFunc() { "
"var a=document.getElementsByTagName('body')[0];"
"alert('%@');"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);",
self.someString];
NSLog(@"insert string %@",insertString);
[self.myWeb stringByEvaluatingJavaScriptFromString:insertString];//插入js
[self.myWeb stringByEvaluatingJavaScriptFromString:@"jsFunc();"];//执行js函数
//var 定义一个变量
js语句主要介绍
document:属性
document.title
//设置文档标题等价于HTML的
document.bgColor
//设置页面背景色
document.fgColor
//设置前景色(文本颜色)
document.linkColor
//未点击过的链接颜色
document.alinkColor
//激活链接(焦点在此链接上)的颜色
document.vlinkColor
//已点击过的链接颜色
document.URL //设置URL属性从而在同一窗口打开另一网页
document.fileCreatedDate
//文件建立日期,只读属性
document.fileModifiedDate
//文件修改日期,只读属性
document.fileSize
//文件大小,只读属性
document.cookie
//设置和读出cookie
document.charset
//设置字符集
简体中文:gb2312
document:方法
document.write()
//动态向页面写入内容
document_createElement_x_x_x(Tag)
//创建一个html标签对象
document.getElementByIdx_xx_x_x(ID)
//获得指定ID值的对象
document.getElementsByName(Name)
//获得指定Name值的对象
document.body.a(oTag)
body:子对象
document.body
//指定文档主体的开始和结束等价于
document.body.bgColor //设置或获取对象后面的背景颜色
document.body.link
//未点击过的链接颜色
document.body.alink
//激活链接(焦点在此链接上)的颜色
document.body.vlink
//已点击过的链接颜色
document.body.text
//文本色
document.body.innerText
//设置…之间的文本
document.body.innerHTML
//设置…之间的HTML代码
document.body.topMargin
//页面上边距
document.body.leftMargin
//页面左边距
document.body.rightMargin
//页面右边距
document.body.bottomMargin
//页面下边距
document.body.background
//背景图片
document.body.a(oTag)
//动态生成一个HTML对象
location:子对象
document.location.hash
// #号后的部分
document.location.host
// 域名+端口号
document.location.hostname
// 域名
document.location.href
// 完整URL
document.location.pathname
// 目录部分
document.location.port
// 端口号
document.location.protocol
// 网络协议(http:)
document.location.search
// ?号后的部分
常用对象事件:
documeny.location.reload()
//刷新网页
document.location.reload(URL)
//打开新的网页
document.location.assign(URL)
//打开新的网页
document.location.replace(URL)
//打开新的网页
selection-选区子对象
document.selection
参考来源:http://blog.sina.com.cn/s/blog_a7c44c880101dmvj.html
url = @"http://42.96.155.42:8080/crm/loginRelationServlet?openId=A786D29EBAD81123313619A2F19B9447&accessToken=8F796D79CE14E4C5A7AC194D8135E2BB&nickname=Mayer";
页面源文件:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>qq_user</title>
</head>
<body>
<a id="qq_userid">1000049</a>
<a id="qq_password">123456</a>
</body>
</html>
现在的目的是取出账号(qq_userid所对应的值)和密码(qq_password所对应的值)。
iOS客户端中倘若获取到网页上的元素的最简单的两个方法:
1.在
shouldStartLoadWithRequest方法中拿到request的url,从而获取url连接后面所附带的参数。
2.利用webView的强大方法
stringByEvaluatingJavaScriptFromString
来获取网页上的具体元素。(而此种方法需要少许知道js语句)
在此,讨论第二种方法。
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *String =
@"UIWebView是iOS最常用的SDK之一,它有一个stringByEvaluatingJavaScriptFromString方法可以将javascript嵌入页面中,通过这个方法我们可以在iOS中与UIWebView中的网页元素交互";
NSString *user =
@"document.getElementById('qq_userid').innerHTML";
NSString *pwd =
@"document.getElementById('qq_password').innerHTML";
NSString *str = [NSString
stringWithFormat:@"document.getElementById('qq_userid').innerHTML ='%@';",String];
NSString *userResult = [self
stringByEvaluatingJavaScriptFromString:user];
NSString *pwdResult = [self
stringByEvaluatingJavaScriptFromString:pwd];
}
至此,可以拿到账号和密码。
警告1 stringByEvaluatingJavaScriptFromString:此方法的使用最好在webViewDidFinishLoad方法中调用
警告2 要想拿某个页面上的元素,必须首先保证此页面加载。(即在
shouldStartLoadWithRequest
方法中必须放回yes)
其实警告1也是基于警告2.
其方法的难点在于js语句的使用。
下面简单的分析下js语句:
getElementById('qq_password') ==通过id获取页面元素。
innerHTML属性 ==获取html当前标签的起始和结束里面的内容
document.getElementById('qq_userid').innerHTML ='%@'; ==会修改所获取到的值。
插入js 并执行js函数
NSString *insertString = [NSString stringWithFormat:
@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function jsFunc() { "
"var a=document.getElementsByTagName('body')[0];"
"alert('%@');"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);",
self.someString];
NSLog(@"insert string %@",insertString);
[self.myWeb stringByEvaluatingJavaScriptFromString:insertString];//插入js
[self.myWeb stringByEvaluatingJavaScriptFromString:@"jsFunc();"];//执行js函数
//var 定义一个变量
js语句主要介绍
document:属性
document.title
//设置文档标题等价于HTML的
document.bgColor
//设置页面背景色
document.fgColor
//设置前景色(文本颜色)
document.linkColor
//未点击过的链接颜色
document.alinkColor
//激活链接(焦点在此链接上)的颜色
document.vlinkColor
//已点击过的链接颜色
document.URL //设置URL属性从而在同一窗口打开另一网页
document.fileCreatedDate
//文件建立日期,只读属性
document.fileModifiedDate
//文件修改日期,只读属性
document.fileSize
//文件大小,只读属性
document.cookie
//设置和读出cookie
document.charset
//设置字符集
简体中文:gb2312
document:方法
document.write()
//动态向页面写入内容
document_createElement_x_x_x(Tag)
//创建一个html标签对象
document.getElementByIdx_xx_x_x(ID)
//获得指定ID值的对象
document.getElementsByName(Name)
//获得指定Name值的对象
document.body.a(oTag)
body:子对象
document.body
//指定文档主体的开始和结束等价于
document.body.bgColor //设置或获取对象后面的背景颜色
document.body.link
//未点击过的链接颜色
document.body.alink
//激活链接(焦点在此链接上)的颜色
document.body.vlink
//已点击过的链接颜色
document.body.text
//文本色
document.body.innerText
//设置…之间的文本
document.body.innerHTML
//设置…之间的HTML代码
document.body.topMargin
//页面上边距
document.body.leftMargin
//页面左边距
document.body.rightMargin
//页面右边距
document.body.bottomMargin
//页面下边距
document.body.background
//背景图片
document.body.a(oTag)
//动态生成一个HTML对象
location:子对象
document.location.hash
// #号后的部分
document.location.host
// 域名+端口号
document.location.hostname
// 域名
document.location.href
// 完整URL
document.location.pathname
// 目录部分
document.location.port
// 端口号
document.location.protocol
// 网络协议(http:)
document.location.search
// ?号后的部分
常用对象事件:
documeny.location.reload()
//刷新网页
document.location.reload(URL)
//打开新的网页
document.location.assign(URL)
//打开新的网页
document.location.replace(URL)
//打开新的网页
selection-选区子对象
document.selection
参考来源:http://blog.sina.com.cn/s/blog_a7c44c880101dmvj.html
相关文章推荐
- iOS开发 ----- webView和JS的简单交互
- IOS 开发OC 与JS 交互 WebviewJavaScriptBridge 的简单实用(二)
- iOS中OC与JS交互WebViewJavascriptBridge的简单使用
- iOS中原生APP与JS交互 之 WebViewJavascriptBridge 的简单用法
- iOS web与js的简单交互
- iOS(WKWebView)OC与JS交互 之三
- iOS解析---WebView和js交互原理
- iOS webView与H5的交互(返回页面的处理)
- IOS JS交互Webview重定向
- iOS OC与JS的交互(WebViewJavascriptBridge实现)
- iOS之网络—— AFN使用、检测网络状态、HTTPS基本使用、WebView基本使用、OC和JS、NSInvocation、异常处理
- iOS中webview和js的交互
- ios下OC与JS交互之WKWebView
- WKWebView的简单使用,与js交互(js调用OC或swift方法)
- iOS-WKWebView特性以及与JS交互的一些事
- iOS解析---WebView和js交互原理
- iOS ios与js交互,获取webview完整url,title,获取元素并赋值跳转
- iOS开发,webView加载图片, js交互, 点击图片放大
- iOS 基于JavaScriptCore 不等webView加载完毕就交互,网页获取原生内容。 webView的高级用法之JS交互,js与oc的相互调用
- ios webView与js交互与类似回调的实现(swift)