由于传统的ios开发中想链接网页需要使用跳转safari或者内置的webview来实现,但是跳转safari会导致当前应用暂时退出,使用内置的webview功能不够强大,比如进度条功能就不怎么尽如人意。因此有很多人开发了一些包来实现webview,下面我就对于我搜索到的一些进行一个小结。
首先是最推荐的,也是我后来使用的SVWebviewController,这个版本在加载网页的时候有一个小轮子显示在加载,而且支持navigation bar的modal和push方法。链接如下:http://bj007.blog.51cto.com/1701577/634942
第二个版本叫做HZWebviewController,这个版本在加载的过程中没有显示,但是基本的网页前进后退都是有的,链接如下:http://code4app.com/ios/HZWebViewController/5343bf20933bf062608b5fdf
还有一个只需要添加函数而不需要导入framework的版本,链接找不到了我就把全文贴上来吧。。可能是我读代码能力不佳,这个方法实验木有成功。。
01.//创建UIWebView
02.WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 320, 400)];
03.[WebView setUserInteractionEnabled:NO];
04.[WebView setBackgroundColor:[UIColor clearColor]];
05.[WebView setDelegate:self];
06.[WebView setOpaque:NO];//使网页透明
07.
08.NSString *path = @"http://www.baidu.com";
09.NSURL *url = [NSURL URLWithString:path];
10.[WebView loadRequest:[NSURLRequest requestWithURL:url]];
11.
12.//创建UIActivityIndicatorView背底半透明View
13.UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
14.[view setTag:103];
15.[view setBackgroundColor:[UIColor blackColor]];
16.[view setAlpha:0.8];
17.[self.view addSubview:view];
18.
19.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
20.[activityIndicator setCenter:view.center];
21.[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
22.[view addSubview:activityIndicator];
23.[self.view addSubview:WebView];
24.[view release];
25.[WebView release];
26.
27.//开始加载数据
28.- (void)webViewDidStartLoad:(UIWebView *)webView {
29. [activityIndicator startAnimating];
30.}
31.
32.//数据加载完
33.- (void)webViewDidFinishLoad:(UIWebView *)webView {
34. [activityIndicator stopAnimating];
35. UIView *view = (UIView *)[self.view viewWithTag:103];
36. [view removeFromSuperview];
37.}
[c] view plaincopy
01.//创建UIWebView
02.WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 320, 400)];
03.[WebView setUserInteractionEnabled:NO];
04.[WebView setBackgroundColor:[UIColor clearColor]];
05.[WebView setDelegate:self];
06.[WebView setOpaque:NO];//使网页透明
07.
08.NSString *path = @"http://www.baidu.com";
09.NSURL *url = [NSURL URLWithString:path];
10.[WebView loadRequest:[NSURLRequest requestWithURL:url]];
11.
12.//创建UIActivityIndicatorView背底半透明View
13.UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
14.[view setTag:103];
15.[view setBackgroundColor:[UIColor blackColor]];
16.[view setAlpha:0.8];
17.[self.view addSubview:view];
18.
19.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
20.[activityIndicator setCenter:view.center];
21.[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
22.[view addSubview:activityIndicator];
23.[self.view addSubview:WebView];
24.[view release];
25.[WebView release];
26.
27.//开始加载数据
28.- (void)webViewDidStartLoad:(UIWebView *)webView {
29. [activityIndicator startAnimating];
30.}
31.
32.//数据加载完
33.- (void)webViewDidFinishLoad:(UIWebView *)webView {
34. [activityIndicator stopAnimating];
35. UIView *view = (UIView *)[self.view viewWithTag:103];
36. [view removeFromSuperview];
37.}
第二种方法:使用UIAlertView and UIActivityIndicatorView
C代码
01.//加载网页动画
02.- (void)webViewDidStartLoad:(UIWebView *)webView{
03. if (myAlert==nil){
04. myAlert = [[UIAlertView alloc] initWithTitle:nil
05. message: @"正在讀取網路資料"
06. delegate: self
07. cancelButtonTitle: nil
08. otherButtonTitles: nil];
09.
10. UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
11. activityView.frame = CGRectMake(120.f, 48.0f, 37.0f, 37.0f);
12. [myAlert addSubview:activityView];
13. [activityView startAnimating];
14. [myAlert show];
15. }
16.}
17.
18.- (void)webViewDidFinishLoad:(UIWebView *)webView{
19. [myAlert dismissWithClickedButtonIndex:0 animated:YES];
20.}
[c] view plaincopy
01.//加载网页动画
02.- (void)webViewDidStartLoad:(UIWebView *)webView{
03. if (myAlert==nil){
04. myAlert = [[UIAlertView alloc] initWithTitle:nil
05. message: @"正在讀取網路資料"
06. delegate: self
07. cancelButtonTitle: nil
08. otherButtonTitles: nil];
09.
10. UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
11. activityView.frame = CGRectMake(120.f, 48.0f, 37.0f, 37.0f);
12. [myAlert addSubview:activityView];
13. [activityView startAnimating];
14. [myAlert show];
15. }
16.}
17.
18.- (void)webViewDidFinishLoad:(UIWebView *)webView{
19. [myAlert dismissWithClickedButtonIndex:0 animated:YES];
20.}
来源: http://www.cocoachina.com/bbs/read.php?tid=9419
用法一:只显示不停旋转的进度滚轮指示器。
C代码
01.//显示进度滚轮指示器
02.-(void)showWaiting {
03. progressInd=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
04. progressInd.center=CGPointMake(self.view.center.x,240);
05. [self.navigationController.view addSubview:progressInd];
06. [progressInd startAnimating];
07.}
08.
09.//消除滚动轮指示器
10.-(void)hideWaiting
11.{
12. [progressInd stopAnimating];
13.}
[c] view plaincopy
01.//显示进度滚轮指示器
02.-(void)showWaiting {
03. progressInd=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
04. progressInd.center=CGPointMake(self.view.center.x,240);
05. [self.navigationController.view addSubview:progressInd];
06. [progressInd startAnimating];
07.}
08.
09.//消除滚动轮指示器
10.-(void)hideWaiting
11.{
12. [progressInd stopAnimating];
13.}
用法二:带有半透明背景的进度轮指示器。
C代码
01.//显示进度滚轮指示器
02.-(void)showWaiting:(UIView *)parent
03.{
04. int width = 32, height = 32;
05. CGRect frame = CGRectMake(100, 200, 110, 70) ;//[parent frame]; //[[UIScreen mainScreen] applicationFrame];
06. int x = frame.size.width;
07. int y = frame.size.height;
08.
09. frame = CGRectMake((x - width) / 2, (y - height) / 2, width, height);
10. UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc]initWithFrame:frame];
11. [progressInd startAnimating];
12. progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
13.
14. frame = CGRectMake((x - 70)/2, (y - height) / 2 + height, 80, 20);
15. UILabel *waitingLable = [[UILabel alloc] initWithFrame:frame];
16. waitingLable.text = @"Loading...";
17. waitingLable.textColor = [UIColor whiteColor];
18. waitingLable.font = [UIFont systemFontOfSize:15];
19. waitingLable.backgroundColor = [UIColor clearColor];
20.
21. frame = CGRectMake(100, 200, 110, 70) ;//[parent frame];
22. UIView *theView = [[UIView alloc] initWithFrame:frame];
23. theView.backgroundColor = [UIColor blackColor];
24. theView.alpha = 0.7;
25.
26. [theView addSubview:progressInd];
27. [theView addSubview:waitingLable];
28.
29. [progressInd release];
30. [waitingLable release];
31.
32. [theView setTag:9999];
33. [parent addSubview:theView];
34. [theView release];
35.
36.}
37.
38.
39.//消除滚动轮指示器
40.-(void)hideWaiting
41.
42.{
43. [[self.view viewWithTag:9999] removeFromSuperview];
44.
45.}
[c] view plaincopy
01.//显示进度滚轮指示器
02.-(void)showWaiting:(UIView *)parent
03.{
04. int width = 32, height = 32;
05. CGRect frame = CGRectMake(100, 200, 110, 70) ;//[parent frame]; //[[UIScreen mainScreen] applicationFrame];
06. int x = frame.size.width;
07. int y = frame.size.height;
08.
09. frame = CGRectMake((x - width) / 2, (y - height) / 2, width, height);
10. UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc]initWithFrame:frame];
11. [progressInd startAnimating];
12. progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
13.
14. frame = CGRectMake((x - 70)/2, (y - height) / 2 + height, 80, 20);
15. UILabel *waitingLable = [[UILabel alloc] initWithFrame:frame];
16. waitingLable.text = @"Loading...";
17. waitingLable.textColor = [UIColor whiteColor];
18. waitingLable.font = [UIFont systemFontOfSize:15];
19. waitingLable.backgroundColor = [UIColor clearColor];
20.
21. frame = CGRectMake(100, 200, 110, 70) ;//[parent frame];
22. UIView *theView = [[UIView alloc] initWithFrame:frame];
23. theView.backgroundColor = [UIColor blackColor];
24. theView.alpha = 0.7;
25.
26. [theView addSubview:progressInd];
27. [theView addSubview:waitingLable];
28.
29. [progressInd release];
30. [waitingLable release];
31.
32. [theView setTag:9999];
33. [parent addSubview:theView];
34. [theView release];
35.
36.}
37.
38.
39.//消除滚动轮指示器
40.-(void)hideWaiting
41.
42.{
43. [[self.view viewWithTag:9999] removeFromSuperview];
44.
45.}