生活随笔
收集整理的這篇文章主要介紹了
使用UIWebView加载网页
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、使用UIWebView加載網頁
運行XCode 4.3,新建一個Single View Application,命名為WebViewDemo。
2、加載WebView
在ViewController.h添加WebView成員變量和在ViewController.m添加實現
[cpp] view plaincopyprint?
#import?<UIKit/UIKit.h> ???? @interface?ViewController?:?UIViewController?? {?? ????UIWebView?*webView;?? }?? @end??
#import <UIKit/UIKit.h>@interface ViewController : UIViewController
{UIWebView *webView;
}
@end
[cpp] view plaincopyprint?
ViewController.m??
ViewController.m
[cpp] view plaincopyprint?
-?(void )viewDidLoad?? {?? ????[super?viewDidLoad];?? ????webView?=?[[UIWebView?alloc]?initWithFrame:CGRectMake(0,?0,?320,?480)];?? ????NSURLRequest?*request?=[NSURLRequest?requestWithURL:[NSURL?URLWithString:@"http://www.baidu.com" ]];?? ????[self.view?addSubview:?webView];?? ????[webView?loadRequest:request];?? }??
- (void)viewDidLoad
{[super viewDidLoad];webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];[self.view addSubview: webView];[webView loadRequest:request];
}運行,這樣百度網頁就打開了
手機的網絡環境是實時變化的,網絡慢的時候,怎么提示用戶網頁正在打開呢?在網頁打開出錯的時候怎么提示用戶呢?這時候我們就需要知道網頁什么時候打開的,
什么時候加載完成,什么時候出錯了。那么我們需要實現這個<UIWebViewDelegate>協議
3、實現協議,在ViewController.h修改如下:
[cpp] view plaincopyprint?
#import?<UIKit/UIKit.h> ???? @interface?ViewController?:?UIViewController<UIWebViewDelegate>?? {?? ????UIWebView?*webView;?? }?? @end??
#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UIWebViewDelegate>
{UIWebView *webView;
}
@end
按住control+command+向上鍵,切換到ViewController.m文件,這是我們在文件中打入- (void) webView,就能看到如下實現方法:
UIWebView中幾個重要的函數 1.- (void )webViewDidStartLoad:(UIWebView ?*)webView ? 網頁開始加載的時候調用 2.- (void )webViewDidFinishLoad:(UIWebView ?*)webView ?網頁加載完成的時候調用 3.- (void)webView:(UIWebView *)webView ?didFailLoadWithError:(NSError *)error 網頁加載錯誤的時候調用
4、實現這三個方法,加入NSLog。
先在viewDidLoad 的webView實例化下面加上
? ??[ webView ? setDelegate: self ];設置代理。這樣上面的三個方法才能得到回調。
三個方法實現如下:
[cpp] view plaincopyprint?
<SPAN?style="FONT-FAMILY:?Arial,?Verdana,?sans-serif;?COLOR:?#333333" >-?(void )?webViewDidStartLoad:(UIWebView?*)webView?? {?? ????NSLog(@"webViewDidStartLoad" );?? }?? -?(void )?webViewDidFinishLoad:(UIWebView?*)webView?? {?? ????NSLog(@"webViewDidFinishLoad" );?? }?? -?(void )?webView:(UIWebView?*)webView?didFailLoadWithError:(NSError?*)error?? {?? ????NSLog(@"didFailLoadWithError:%@" ,?error);?? }?? </SPAN>??
- (void) webViewDidStartLoad:(UIWebView *)webView
{NSLog(@"webViewDidStartLoad");
}
- (void) webViewDidFinishLoad:(UIWebView *)webView
{NSLog(@"webViewDidFinishLoad");
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{NSLog(@"didFailLoadWithError:%@", error);
}
運行打印:
2012-06-23 15:20:29.728 WebViewDemo[1001:f803] webViewDidStartLoad
2012-06-23 15:20:29.991 WebViewDemo[1001:f803] webViewDidFinishLoad
那我們試試error情況,把wifi關掉,運行打印結果:
2012-06-23 15:23:58.939 WebViewDemo[1087:f803] webViewDidStartLoad
2012-06-23 15:23:59.016 WebViewDemo[1087:f803] webViewDidFinishLoad
請求結果不變,為什么關掉網絡還成功了呢?緩存?我換163.com試試,這是真正的結果出來了:
2012-06-23 15:24:41.131 WebViewDemo[1134:f803] webViewDidStartLoad
2012-06-23 15:24:41.149 WebViewDemo[1134:f803] didFailLoadWithError:Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo=0x6b41660 {NSErrorFailingURLStringKey=http://www.163.com/, NSErrorFailingURLKey=http://www.163.com/, NSLocalizedDescription=The Internet connection appears to be offline., NSUnderlyingError=0x6eae690 "The Internet connection appears to be offline."}
連接錯誤了,調用了
didFailLoadWithError。
5、加載等待界面
為了給用戶更直觀的界面效果,我們加上等待的loading界面試試
在webViewDidStartLoad加入等待
[cpp] view plaincopyprint?
<STRONG>-?(void )?webViewDidStartLoad:(UIWebView?*)webView?? {?? ?????? ????UIView?*view?=?[[UIView?alloc]?initWithFrame:CGRectMake(0,?0,?320,?480)];???? ????[view?setTag:108];???? ????[view?setBackgroundColor:[UIColor?blackColor]];???? ????[view?setAlpha:0.5];???? ????[self.view?addSubview:view];???? ?????? ????activityIndicator?=?[[UIActivityIndicatorView?alloc]?initWithFrame:CGRectMake(0.0f,?0.0f,?32.0f,?32.0f)];???? ????[activityIndicator?setCenter:view.center];???? ????[activityIndicator?setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];???? ????[view?addSubview:activityIndicator];???? ?? ????[activityIndicator?startAnimating];?? ???</STRONG>??
- (void) webViewDidStartLoad:(UIWebView *)webView
{//創建UIActivityIndicatorView背底半透明View UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; [view setTag:108]; [view setBackgroundColor:[UIColor blackColor]]; [view setAlpha:0.5]; [self.view addSubview:view]; activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)]; [activityIndicator setCenter:view.center]; [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite]; [view addSubview:activityIndicator]; [activityIndicator startAnimating];
加載完成或失敗時,去掉loading效果
[cpp] view plaincopyprint?
<STRONG>-?(void )?webViewDidFinishLoad:(UIWebView?*)webView?? {?? ????[activityIndicator?stopAnimating];?? ????UIView?*view?=?(UIView*)[self.view?viewWithTag:108];?? ????[view?removeFromSuperview];?? ????NSLog(@"webViewDidFinishLoad" );?? ?? }?? -?(void )?webView:(UIWebView?*)webView?didFailLoadWithError:(NSError?*)error?? {?? ????[activityIndicator?stopAnimating];?? ????UIView?*view?=?(UIView*)[self.view?viewWithTag:108];?? ????[view?removeFromSuperview];?? ????</STRONG>??
- (void) webViewDidFinishLoad:(UIWebView *)webView
{[activityIndicator stopAnimating];UIView *view = (UIView*)[self.view viewWithTag:108];[view removeFromSuperview];NSLog(@"webViewDidFinishLoad");}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{[activityIndicator stopAnimating];UIView *view = (UIView*)[self.view viewWithTag:108];[view removeFromSuperview];
運行效果:
總結
以上是生活随笔 為你收集整理的使用UIWebView加载网页 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。