使用WKWebview时导入
#import
使用webView时,加载js时,js代码中不能有变量表示,
wkwebView,加载js 时,代码中可以用变量;
参考:http://blog.csdn.net/u011154007/article/details/52944767
在网页加载完成的时候====
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
//获取整个网页的HTML代码
NSString *doc =@"document.body.outerHTML";
[webView evaluateJavaScript:doc
completionHandler:^(id_Nullable htmlStr,NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"html--:%@",htmlStr);
}] ;
//获取当前页面的url
NSString *doc01 =@"document.location.href";
[webView evaluateJavaScript:doc01
completionHandler:^(id_Nullable htmlStr,NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"url--:%@",htmlStr);
}] ;
//获取页面的title
NSString *doc02 =@"document.title";
[webView evaluateJavaScript:doc02
completionHandler:^(id_Nullable htmlStr,NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"url--:%@",htmlStr);
}] ;
//修改界面的元素的值
NSString *doc03 =@"document.getElementsByName('q')[0].value='朱祁林';";
[webView evaluateJavaScript:doc03
completionHandler:^(id_Nullable htmlStr,NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"修改元素--:%@",htmlStr);
}] ;
//提交表单
NSString *doc04 =@"document.forms[0].submit();";
[webView evaluateJavaScript:doc04
completionHandler:^(id_Nullable htmlStr,NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"提交表单--:%@",htmlStr);
}] ;
//获取所有的HTML
NSString *doc05 =@"document.documentElement.innerHTML";
[webView evaluateJavaScript:doc05
completionHandler:^(id_Nullable htmlStr,NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"获取所有的HTML--:%@",htmlStr);
}] ;
//获取网页的一个值
NSString *doc06 =@"document.getElementById('title').innerText";
[webView evaluateJavaScript:doc06
completionHandler:^(id_Nullable htmlStr,NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"获取网页的一个值--:%@",htmlStr);
}] ;
//获取网页内容的高度
[webView evaluateJavaScript:@"document.body.offsetHeight;"
completionHandler:^(id _Nullable any, NSError * _Nullable error) {
NSString *heightStr = [NSString stringWithFormat:@"%@",any];
}];
NSLog(@"完成加载");
}
if ([title compare: @"Google"]==NSOrderedSame ) {
[webView evaluateJavaScript:@"var script = document.createElement_x('script');"
"script.type = 'text/javascript';"
"script.text = "function myFunction() {"
"var field = document.getElementsByName('q')[0];"
"field.value='朱祁林';"
"document.forms[0].submit();"
"}";"
"document.getElementsByTagName_r('head')[0].appendChild(script);"
completionHandler:^(id _Nullable htmlStr, NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"获取网页的一个值--:%@",htmlStr);
}] ;
];
[webView evaluateJavaScript:@"myFunction();"
completionHandler:^(id _Nullable htmlStr, NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"获取网页的一个值--:%@",htmlStr);
}] ;
];}
看上面的代码:
a、首先通过js创建一个script的标签,type为'text/javascript'。
b、然后在这个标签中插入一段字符串,这段字符串就是一个函数:myFunction,这个函数实现google自动搜索关键字的功能。
c、然后使用webView evaluateJavaScript执行myFunction函数。
[webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"var edit_card_num = setInterval(function () {if (document.getElementById('cardNumber')) {cardNumber = document.getElementById('cardNumber');cardNumber.value = '6216261000000000018';cardNumber.setAttribute('disabled', 'disabled');if (document.getElementsByClassName('up-btn next cardbin')[0]) {document.getElementsByClassName('up-btn next cardbin')[0].click();clearInterval(edit_card_num)}}}, 5)\";"
"document.body.appendChild(script);"];
NSString *jsStr=[NSString stringWithFormat:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"%@\";"
"document.body.appendChild(script);",self.jsStr];
[webView stringByEvaluatingJavaScriptFromString:jsStr];
=======webview 修改网页====
-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *currentURL = [webViewstringByEvaluatingJavaScriptFromString:@"document.location.href"];
NSLog(@"--wangzhi--%@",currentURL);
NSString *allHtml =@"document.getElementsByTagName('html')[0].innerHTML";
NSString *allhtml=[webViewstringByEvaluatingJavaScriptFromString:allHtml];
NSLog(@"所有的html%@",allhtml);
NSString *fieldStr=[webViewstringByEvaluatingJavaScriptFromString:@"document.getElementsById('session_email_or_mobile_number').value
'sssss'
;" ];NSLog(@"strv输入框%@",fieldStr);
NSString *title = [webViewstringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"标题--%@",title);
[webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement_x('script');"
"script.type = 'text/javascript';"
"script.text = \"function myFunction() {\"var field = document.getElementsById('session_email_or_mobile_number');"
"field.value='朱祁林';"
"document.forms[0].submit();"
"}\";"
"document.getElementsByTagName_r('head')[0].appendChild(script);"];
[webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];
}
**********************
NSString * htmlStr =[NSStringstringWithFormat:
@"
\n""\";",url,version,agent,mobile,debit,name,
idno,credit,cvn,expiry,out_order_id,amount,front_url,order_id,sumer_fee,noise,sign];
================
html代码:
window.onload = function () {
// 判断设备
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
var timer = null;
if (isAndroid) { // android
window.android.cashout();
}
if (isIOS) { // ios
alert('开启定时器');
var timer = setInterval(function () {
if (cashout) {
alert('函数加载完成');
clearInterval(timer);
cashout();
}
}, 100);
}
}
*******OC代码
-(void)setWebviewWithurlStr:(NSString *)urlStr{
UIWebView *web=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64)];
if(HEIGHT==812){
web=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-88-34)];
}
self.web=web;
web.delegate=self;
// web.UIDelegate=self;
// web.navigationDelegate=self;
[self.view addSubview:web];
NSURL *url=[NSURL URLWithString:urlStr];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[web loadRequest:request];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
self.index++;
if(self.index==2){
//这个是返回的url是真个HTML代码。
NSString *url=[NSString stringWithContentsOfURL:request.URL encoding:NSUTF8StringEncoding error:nil];
NSLog(@"--url ---%@",url);
[self.web loadHTMLString:url baseURL:nil];
}
return YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
[[LYCommonMBprogresshud sharedManager]dimisAnimalProgressWith:self.view];
JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context[@"cashout"]=^(){
NSLog(@"HTML执行了");
dispatch_async(dispatch_get_main_queue(), ^{
if([self.frommark isEqualToString:getMoneySelectVC]){
//到信用卡列表
LYWLBCreditListVC *wlbcreditlist=[[LYWLBCreditListVC alloc]init];
[self.navigationController pushViewController:wlbcreditlist animated:YES];
}else {//如果是从银行卡列表过来,返回到银行卡列表
if([self.delegate respondsToSelector:@selector(LYWLBBindCreditVC:)]){
[self.delegate LYWLBBindCreditVC:self];
[self.navigationController popViewControllerAnimated:YES];
}
}
});
};
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[[LYCommonMBprogresshud sharedManager]dimisAnimalProgressWith:self.view];
[[LYCommonMBprogresshud sharedManager]showProgresshudTextWith:self.view title:@"加载失败" detailtitle:@"" isOnDismissbg:NO hideAfterdelay:YES];
NSLog(@"error---%@",error);
}
- (void)dealloc {
self.web.delegate = nil;
[self.web stopLoading];
}