UIWebView之网络状况显示是本文介绍的内容,如果直接用UIWebView来显示网页很简单,只需要向UIWebView发送一个NSURLRequest即可。但是你会发现,不够人性化,因为你不知道当前状况,网页是否在加载,是否已加载完。mobile safari就很有人性化,不但有blue progressbar,还有一个network activity indicator在status bar。
那么我们也需要加入这样的特色到应用当中。加入network activity indicator其实很简单,代码如下:
- - (void)showLoading
- {
- [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
- self.theActivityView.hidden = NO;
- [self.theActivityView startAnimating];
- }
- - (void)hideLoading
- {
- [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
- self.theActivityView.hidden = YES;
- [self.theActivityView stopAnimating];
- }
- - (void)webViewDidStartLoad:(UIWebView *)webView
- {
- [self showLoading];
- NSLog(@"start load");
- }
- - (void)webViewDidFinishLoad:(UIWebView *)webView
- {
- [self hideLoading];
- NSLog(@"finish load");
- }
- - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
- {
- [self hideLoading];
- NSLog(@"error load");
- }
- - (void)showLoading
- {
- [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
- self.theActivityView.hidden = NO;
- [self.theActivityView startAnimating];
- }
- - (void)hideLoading
- {
- [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
- self.theActivityView.hidden = YES;
- [self.theActivityView stopAnimating];
- }
- - (void)webViewDidStartLoad:(UIWebView *)webView
- {
- [self showLoading];
- NSLog(@"start load");
- }
- - (void)webViewDidFinishLoad:(UIWebView *)webView
- {
- [self hideLoading];
- NSLog(@"finish load");
- }
- - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
- {
- [self hideLoading];
- NSLog(@"error load");
- }
theActivityView是UIActivityIndicatorView的实例。 这样当UIWebView加载网页的时候,就会在status bar有一个indicator, 在UIWebView frame当中还有一个UIActivityIndicatorView以示网页正在加载。
研究了一下如何实现progressbar,还没找到方法。因为UIWebView可用的delegate方法只有那么几个,对进度条显示没有帮助,只有另寻他法。
小结:详解UIWebView之网络状况显示的内容介绍完了,希望通过本文的学习能对你有所帮助。