IOS系列——加载网页webView


    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
    webView.backgroundColor = [UIColor clearColor];
    webView.scalesPageToFit =YES;
    webView.delegate =self;
    
    activity = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 64, 64)];
    [activity setCenter:self.view.center];
    [activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [activity startAnimating];
    [webView addSubview:activity];     //加在webView的话,不用担心action被webView遮盖
    
    NSURL *url = [[NSURL alloc]initWithString:@"http://www.cv111.cc/yjw/news1.asp"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [webView loadRequest:request];
    [self.view addSubview:webView];


- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [activity startAnimating] ;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   [activity stopAnimating];
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请求不成功,请查看网络连接" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}

完整代码就是这样的,当然,我在做项目的时候,遇到这样一个问题,我在一个SCrollView上加载了两个webView,在第二个页面的时候,写
[activity setCenter:self.view.center];
这一句的时候,action始终都显示不出来,后来改成

activity setCenter:webView.center  就让这个显示出来了


如果要加载本地html的话

    //加载本地url
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"img/品牌/di" ofType:@"jpg"];
    NSData *image = [NSData dataWithContentsOfFile:filePath];
    UIImageView*img_bg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 1024, 768)];
    [img_bg setImage:[UIImage imageWithData:image]];
    [self.view addSubview:img_bg];
//    [self showContent];
    UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    NSString *basePath = [[NSBundle mainBundle]bundlePath];
    NSString *helpHtmlPath = [basePath stringByAppendingPathComponent:@"img/动态/新闻/news.html"];
    NSURL *url = [NSURL fileURLWithPath:helpHtmlPath];
    [web loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:web];


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值