iOS 8:UIWebView的简单使用
1 基本用法
1、实例化:UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.window.bounds];
2、配置参数:delegate、scalesPageToFit等
3、加载请求:[webView loadRequest:NSRequest实例];
4、委托方法:
1)、完成加载
- (void)webViewDidFinishLoad:(UIWebView *)webView
2 操作JavaScript
1、获得网页地址
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
2、获得网页标题
NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
3、修改字体大小
NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName(‘body‘)[0].style.webkitTextSizeAdjust= ‘%d%%‘",
50];
[webView stringByEvaluatingJavaScriptFromString:jsString];
4、水平垂直居中
NSString *bodyStyleVertical = @"document.getElementsByTagName(‘body‘)[0].style.verticalAlign = ‘middle‘;";
NSString *bodyStyleHorizontal = @"document.getElementsByTagName(‘body‘)[0].style.textAlign = ‘center‘;";
NSString *mapStyle = @"document.getElementById(‘mapid‘).style.margin = ‘auto‘;";
[webView stringByEvaluatingJavaScriptFromString:bodyStyleVertical];
[webView stringByEvaluatingJavaScriptFromString:bodyStyleHorizontal];
[webView stringByEvaluatingJavaScriptFromString:mapStyle];
5、修改字体颜色
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName(‘body‘)[0].style.webkitTextFillColor= ‘red‘"];
6、修改页面背景颜色
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName(‘body‘)[0].style.background=‘black‘"];
7、改变图片大小
1 [webView stringByEvaluatingJavaScriptFromString: 2 @"var script = document.createElement(‘script‘);" 3 "script.type = ‘text/javascript‘;" 4 "script.text = \"function ResizeImages() { " 5 "var myimg,oldwidth;" 6 "var maxwidth=320;" //缩放系数 7 "for(i=0;i <document.images.length;i++){" 8 "myimg = document.images[i];" 9 "if(myimg.width > maxwidth){" 10 "oldwidth = myimg.width;" 11 "myimg.width = maxwidth;" 12 "myimg.height = myimg.height * (maxwidth/oldwidth);" 13 "}" 14 "}" 15 "}\";" 16 "document.getElementsByTagName(‘head‘)[0].appendChild(script);"]; 17 18 [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
8、提交表单
1 NSString * currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"]; 2 //取得标题 3 NSString * title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"]; 4 //设置表单 5 NSString * js_result = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName(‘q‘)[0].value=‘username‘;"]; 6 //提交表单 7 NSString * js_result2 = [webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit();"]; 8 9 NSLog(@"pp%@,qq%@,ww%@,ee%@",currentURL,title,js_result,js_result2);
9、查看源代码
NSString *jsToGetHTMLSource = @"document.getElementsByTagName(‘html‘)[0].innerHTML";
NSString *HTMLSource = [webView_ stringByEvaluatingJavaScriptFromString:jsToGetHTMLSource];
NSLog(@"%@",HTMLSource);
10、长按菜单
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect=‘none‘;"];
11、滚动到某个位置
[webView stringByEvaluatingJavaScriptFromString:@"window.scrollTo(0,200);"];
12、获取图片手势
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTouchesRequired = 2;
[webView addGestureRecognizer:doubleTap];
13、获取图片地址
1 - (void) doubleTap:(UITapGestureRecognizer*) sender { 2 // <Find HTML tag which was clicked by user> 3 // <If tag is IMG, then get image URL and start saving> 4 UIWebView *webView = (UIWebView*)[self.window viewWithTag:1000]; 5 int scrollPositionY = [[webView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue]; 6 int scrollPositionX = [[webView stringByEvaluatingJavaScriptFromString:@"window.pageXOffset"] intValue]; 7 8 int displayWidth = [[webView stringByEvaluatingJavaScriptFromString:@"window.outerWidth"] intValue]; 9 CGFloat scale = webView.frame.size.width / displayWidth; 10 11 CGPoint pt = [sender locationInView:webView]; 12 pt.x *= scale; 13 pt.y *= scale; 14 pt.x += scrollPositionX; 15 pt.y += scrollPositionY; 16 17 NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).tagName", pt.x, pt.y]; 18 NSString * tagName = [webView stringByEvaluatingJavaScriptFromString:js]; 19 if ([tagName isEqualToString:@“IMG”]) {// IMG可能会换 20 NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y]; 21 NSString *urlToSave = [webView stringByEvaluatingJavaScriptFromString:imgURL]; 22 NSLog(@"image url=%@", urlToSave); 23 } 24 25 26 }
14、获取视频地址
1 NSString *lJs2 = @"(document.getElementsByTagName(\"video\")[0]).getElementsByTagName(\"source\")[0].src"; //qiyi 2 NSString *lJs2 = @"(document.getElementsByTagName(\"video\")[0]).src"; // youku,tudou,ku6 ,souhu 3 NSString *lm3u8 = [webView stringByEvaluatingJavaScriptFromString:lJs2]; 4 NSLog(@"video source:%@",lm3u8);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。