UIWebview的基本使用,并且自带加载框
- (void)viewDidLoad
{
[super viewDidLoad];
//设置webView的代理
_webView.delegate = self;
_indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_indicator.center = self.view.center;
_indicator.hidesWhenStopped = YES;
[self.view addSubview:_indicator];
}
- (IBAction)gotoWebsite:(UIButton *)sender
{
//UIWebView 网页视图,能够加载一个网页
NSURL *url = [NSURL URLWithString:_textFIeld.text];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//loadRequest 加载一个请求
[_webView loadRequest:request];
[_textFIeld resignFirstResponder];
}
- (IBAction)openFIleClick:(UIButton *)sender
{
//webView除了加载网页,还可以浏览一些常用的类型文件(pdf,doc,jpg,txt)等
NSString *path = [[NSBundle mainBundle]pathForResource:@"YiChat" ofType:@"pdf"];
//生成一个本地文件的URL
NSURL *url = [[NSURL alloc]initFileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//loadRequest 加载一个请求
[_webView loadRequest:request];
[url release];
}
//webView 将要加载一个请求时调用,返回YES继续加载,返回NO停止加载
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"%@",request.URL);
return YES;
}
//加载已经开始时调用
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[_indicator startAnimating];
}
//加载完成时调用
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[_indicator stopAnimating];
}
//加载失败时调用
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[_indicator stopAnimating];
}