UIWebview详解

一、
1:NSURL初始化方法:

NSURL *url=[NSURL URLWithString:@"http://www.baidu.com?id=1"];  

2:解决NSURL初始化失败的相关解决方案.
将传进来的NSString 进行 UTF8 转码即可.
①:针对 URLWithString 初始化失败的解决方案

NSString *strLocalHtml = @"file:///Users/amarishuyi/Desktop/My IPhone Life/WebDeveloper/WebPlug-in/ExtEditor/DataPage/KMQT/Ext-HTMLEditor.html";  
strLocalHtml = [NSString stringWithFormat:@"%@?Value=%@",strLocalHtml,self.txtUrl.text];  
strLocalHtml= [strLocalHtml stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
NSURL * url=[NSURL URLWithString:strLocalHtml];  

②:针对 fileURLWithPath 初始化失败的解决方案

self.filePathString = [self.filePathString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
NSURL *url = [NSURL fileURLWithPath:self.filePathString]; 
转码成功后 会自动 在字符串左侧添加 "file:///" 

3:NSURL 成功初始化后可以获取的参数

NSURL *url = [NSURL URLWithString:  @"http://www.baidu.com/s?tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709"];     
NSLog(@"Scheme: %@", [url scheme]);    
NSLog(@"Host: %@", [url host]);    
NSLog(@"Port: %@", [url port]);        
NSLog(@"Path: %@", [url path]);        
NSLog(@"Relative path: %@", [url relativePath]);   
NSLog(@"Path components as array: %@", [url pathComponents]);           
NSLog(@"Parameter string: %@", [url parameterString]);      
NSLog(@"Query: %@", [url query]);          
NSLog(@"Fragment: %@", [url fragment]);  
NSLog(@"User: %@", [url user]);  
NSLog(@"Password: %@", [url password]);  
2012-03-31 18:22:20.904 SmallDemoList[5473:11603] 12131232  
2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Scheme: http  
2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Host: www.baidu.com  
2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Port: (null)  
2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Path: /s  
2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Relative path: /s  
2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Path components as array: (  
    "/",  
    s  
)  
2012-03-31 18:22:20.916 SmallDemoList[5473:11603] Parameter string: (null)  
2012-03-31 18:22:20.917 SmallDemoList[5473:11603] Query: tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709  
2012-03-31 18:22:20.917 SmallDemoList[5473:11603] Fragment: (null)  
2012-03-31 18:22:20.917 SmallDemoList[5473:11603] User: (null)  
2012-03-31 18:22:20.917 SmallDemoList[5473:11603] Password: (null)  

4:根据文件名称和文件后缀获取程序包内容文件的路径

NSURL *urlKindEditor = [[NSBundlemainBundle]URLForResource:@"simple"withExtension:@"html"subdirectory:@"KindEditor/examples"]; 

URLForResource:文件名称
withExtension:文件后缀
subdirectory:在程序包中的哪个子目录中寻找.
如果没有找到将会返回nil
找到后返回如下路径: file://localhost/Users/amarishuyi/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/FB0CDABC-D0E2-45FF-AA2C-959E8A65ADB4/SmallDemoList.app/KindEditor/examples/simple.html

5:对比两个URL 是否相等

url isEqual:[_audioPlayer url]]  

二、
1.网页执行js代码

 stringByEvaluatingJavaScriptFromString

这个方法是让一切成为可能的关键
①从网页获取URL:

 NSString *urlString = [self stringByEvaluatingJavaScriptFromString:@"location.href"];

②从网页获取标题:

[webView stringByEvaluatingJavaScriptFromString:@"document.title"];

③网页的滚动位置:

 CGPoint pt;
pt.x = [[self stringByEvaluatingJavaScriptFromString:@"window.pageXOffset"] integerValue];
pt.y = [[self stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] integerValue];

2.网站的图标Icon下载地址

 NSURL *url = [[NSURL alloc] initWithScheme:[web.request.URL scheme] host:[web.request.URL host] path:@"/favicon.ico"];

3.调整webView里的字体大小

[NSString stringWithFormat:@"<html> \n" 
                                 "<head> \n" 
                                 "<style type=\"text/css\"> \n" 
                                 "body {font-family: \"%@\"; font-size: %f; color: %@;}\n" 
                                 "</style> \n" 
                                 "</head> \n"
                                 "<body>%@</body> \n"
                                 "</html>", @"宋体", fontSize,fontColor,stringValue]

4.改变字体大小颜色

NSString *jsString = [[NSString alloc] initWithFormat:@"document.body.style.fontSize=%f;document.body.style.color=%@",fontSize,fontColor]; 
        [webView stringByEvaluatingJavaScriptFromString:jsString]; 
        [jsString release]; 

5.地址栏随着网页下拉移动,类似safari:
①先拿到webView的scrollView,如果5.0以下就用靠循环去找了

  -(UIScrollView *)getWebScrollView{
        UIScrollView* scroll = nil;


        if ([self respondsToSelector:@selector(scrollView)]) {
            scroll = [self scrollView];
        }else{
            for (UIView* view in [self subviews]) {
                if ([view isKindOfClass:[UIScrollView class]]) {
                    scroll = (UIScrollView*)view;
                    break;
               }
            }
        }
        return scroll;
    }

②然后把设置delegate: WebView.scrollView.delegate = self;
实现scrollViewDidScroll方法:

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{


        CGPoint contentOffset = scrollView.contentOffset;

        CGFloat threshold = self.topBar.frame.size.height; // topBar就是webview上面的地址栏

        if(contentOffset.y>=-threshold &&contentOffset.y<=480 )  // 480设置得有些大了,这里是为了防止快速滑动的时候回调会跟不上,
        {
            self.topBar.frame = CGRectMake(0,-threshold-contentOffset.y, 320, self.topBar.frame.size.height);
        }
        else if(contentOffset.y<-threshold){
            self.topBar.frame = CGRectMake(0, 0, 320, self.topBar.frame.size.height);
        }
    }

6.停止和播放网页上的视频:
iPad上在网页里播放视频,即使关掉webView,依旧余音绕耳,调用下面的js可以停止播放

    function stopVideo(){    var videos = document.querySelectorAll("video");
       for (var i = videos.length - 1; i >= 0; i--){
            videos.pause();
        };
        return 'stop';
    }

7.查找webView中的scrollview:

- (void) addScrollViewListener
{
UIScrollView* currentScrollView;
for (UIView* subView in self.webView.subviews) {
if ([subView isKindOfClass:[UIScrollView class]]) {
currentScrollView = (UIScrollView*)subView;
currentScrollView.delegate = self;
}
}
}

8.去掉webView的阴影,做成类似scrollView:

- (void)clearBackgroundWithColor:(UIColor*)color
{
// 去掉webview的阴影
self.backgroundColor = color;
for (UIView* subView in [self subviews])
{
if ([subView isKindOfClass:[UIScrollView class]]) {
for (UIView* shadowView in [subView subviews])
{
if ([shadowView isKindOfClass:[UIImageView class]]) {
[shadowView setHidden:YES];
}
}
}
}
}

9.取消长按webView上的链接弹出actionSheet的问题:

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout = 'none';"];
}

10取消webView上的超级链接加载问题:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType==UIWebViewNavigationTypeLinkClicked) {
return NO;
}
else {
return YES;
}
}

11.在使用webView进行新浪微博分享时,webView会自动保存登陆的cookie导致项目中的分享模块有些问题,删除 webView的cookie的方法:

-(void)deleteCookieForDominPathStr:(NSString *)thePath
{
//删除本地cookie,thePath为cookie路径通过打印cookie可知道其路径
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {

if([[cookie domain] isEqualToString:thePath]) {

[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
}

12.

三.document:属性

document.title                 //设置文档标题等价于HTML的<title>标签
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(Tag)           //创建一个html标签对象
document.getElementByIdx_x_x(ID)           //获得指定ID值的对象
document.getElementsByName(Name)      //获得指定Name值的对象
document.body.a(oTag)

四.body:子对象

document.body                   //指定文档主体的开始和结束等价于<body></body>
document.body.bgColor           //设置或获取对象后面的背景颜色
document.body.link              //未点击过的链接颜色
document.body.alink             //激活链接(焦点在此链接上)的颜色
document.body.vlink             //已点击过的链接颜色
document.body.text              //文本色
document.body.innerText         //设置<body>...</body>之间的文本
document.body.innerHTML         //设置<body>...</body>之间的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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值