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];