iOS控件:UIWebView的简单使用

一、UIWebView的常用属性

@property (nonatomic) BOOL scalesPageToFit;//是否自适应webView的大小
@property (nonatomic) UIDataDetectorTypes dataDetectorTypes
/*
     UIDataDetectorTypePhoneNumber //自动检测网页上的电话号码
     UIDataDetectorTypeLink//自动检测网页上的连接
     UIDataDetectorTypeAddress//自动检测网页上的地址
     UIDataDetectorTypeCalendarEvent//自动检测网页上的日历事件
     UIDataDetectorTypeNone//不检测任何特殊内容
     UIDataDetectorTypeAll//检测所有特殊内容
     */

二、 UIWebView的加载方式

- (void)loadRequest:(NSURLRequest *)request;//加载并显示url指定网页
- (void)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;//加载并显示指定HTML字符串

加载url举例:

    UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 400)];
    NSString * str = @"http://www.baidu.com";
    NSURL * url = [NSURL URLWithString:str];
    NSURLRequest * requst = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requst];
    webView.delegate = self;

三、UIWebView常用控制

- (void)reload;//重新加载页面
- (void)stopLoading;//停止加载页面
- (void)goBack;//回到上个页面
- (void)goForward;//进入下个页面

四、UIWebView的代理方法

<UIWebViewDelegate>
//是否加载页面
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    <pre name="code" class="objc"><span style="white-space:pre">	</span>//如果加载的HTML页面里有JS事件,会在此方法里捕获。
//以字符串的方式返回js中的方法名 NSString *requestString = [[request URL ] absoluteString ];
return YES;}//开始加载页面-(void)webViewDidStartLoad:(UIWebView *)webView{ }//完成加载页面-(void)webViewDidFinishLoad:(UIWebView *)webView{ //在cell里嵌套webView需要执行如下操作// CGFloat scrollHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue]; //获取加载完成后的webview的高度 CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue]; CGRect frame = webView.frame; frame.size.height = height; webView.frame = frame; //刷新cell的高度 _webViewHeight = height; [self.tableView beginUpdates]; [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic]; [self.tableView endUpdates]; }//页面加载失败-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ }
 

五、UIWebView的其它方法
    //是否显示水平方向滚动条
    webView.scrollView.showsHorizontalScrollIndicator = NO;
    //是否显示垂直方向滚动条
    webView.scrollView.showsVerticalScrollIndicator = NO;
    //是否禁止滚动
    webView.scrollView.scrollEnabled = NO;


六、UIWebView与UITableView嵌套使用

问题一:当UIWebView与UITableView嵌套使用时的一个问题就是UIWebView的高度问题,我们所希望的是UIWebView的高度与cell的高度都等于UIWebView的内容的高度从而将UIWebView的滑动禁止后显示的内容是完全的。

解决办法:

-(void)webViewDidFinishLoad:(UIWebView *)webView{
#if 1
    CGFloat offSetHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    CGRect webViewFrame = self.webView.frame;
    webViewFrame.size.height = offSetHeight;
    self.webView.frame = webViewFrame;
    //_webViewHeight成员变量是UIWebView的初始值
    //刷新tableView时,行高返回_webViewHeight。
    _webViewHeight = offSetHeight;
#else
    CGRect frame = webView.frame;
    frame.size.height = 1;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;
    _webViewHeight = self.webView.frame.size.height;

#endif
    
    
    [self.tableView beginUpdates];
    //刷新对应的cell
    //此处需要避免无限循环。webView添加到cell上 webview是在设置cell时创建的当刷新添加了webview的cell时由于webview的请求问题会造成循环引用,所以[NSIndexPath indexPathForRow:0 inSection:0]],0对应的cell不能是添加了webview的cell;(也可以用重用池原理解决此问题)
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
    
    //设置一个标识 证明已经请求过一次,后面会用到。
    NSString * urlStr = webView.request.URL.absoluteString;
    if ([urlStr isEqualToString:@"http://www.baidu.com"]) {
       self.isFinishLoading = YES;
    }
    
    
    
}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        return 300;
    }
    else if (indexPath.row == 1){
        
        return _webViewHeight;
    }
    return 100;
    
}



问题二:另外一个问题是UIWebView的重复请求问题,由于webview内容的丰富往往需要消耗大量的流量所以要避免重复请求。

解决方法:

思路一:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        MyTableViewCell * cell = [MyTableViewCell cellWithTableView:tableView];
        return cell;
    }
    else if(indexPath.row == 1){
        //要避免cell的重复创建和webView的重复创建及重复请求,cell只创建一次并存储,当cell再次被滑动出来时不是重新创建而是加载已经存储的。
#if 0
        
        if (self.mArray.count == 0) {
            
            UITableViewCell * cell = [[UITableViewCell alloc] init];
            UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, _webViewHeight)];
            NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
            NSURLRequest * request = [NSURLRequest requestWithURL:url];
            [webView loadRequest:request];
            self.webView = webView;
            self.webView.userInteractionEnabled = NO;
            self.webView.delegate = self;
            [cell.contentView addSubview:webView];
            [self.mArray addObject:cell];

        }
        
        
        return [self.mArray lastObject];
#else
        static NSString * ad = @"noRepeat";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ad];
        if (!cell) {
            
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ad];
            UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, _webViewHeight)];
            NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
            NSURLRequest * request = [NSURLRequest requestWithURL:url];
            [webView loadRequest:request];
            self.webView = webView;
            self.webView.userInteractionEnabled = NO;
            self.webView.delegate = self;
            [cell.contentView addSubview:webView];
        }
        
        return cell;
#endif
        
        
    }
    else{
        
        static NSString * cellId = @"systemCell";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            
        }
        cell.textLabel.text = @"骚客足球";
    
        return cell;
    }
    


思路二:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    //如果此网页已经加载过一次就不在加载
    //只是提供一个思路 还有许多细节需要处理
    NSString * urlStr = request.URL.absoluteString;
    BOOL bl = [urlStr isEqualToString:@"http://www.baidu.com"];
    if (bl && self.isFinishLoading) {
        return  NO;
    }
    return YES;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值