您的位置:首页 > 移动开发

UIWebview基本使用(原生加载框UIActivityIndicatorView)

2016-07-04 20:29 495 查看
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];
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: