顯示網頁有兩種方式,第一種是跳出APP,在網頁瀏覽器上顯示,第二種是不跳出APP,在webView上顯示,在此來實作後者。
在.h中宣告好IB需使用到的變數和代理:
@interface HTWebViewController : UIViewController <UIWebViewDelegate> @property (nonatomic, retain) IBOutlet UIWebView *webView; @property (nonatomic, retain) IBOutlet UILabel *statusLabel; @end
在.m中實作對應事件的處理:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupWebView];
self.webView.delegate = self;
self.navigationController.navigationBarHidden = YES;
}
-(void)setupWebView
{
NSURL *url = [NSURL URLWithString:@"https://cg2010studio.wordpress.com/"];
NSURLRequest *request= [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
#pragma mark - UIWebView Delegate Methods
-(void)webViewDidStartLoad:(UIWebView *)webView
{
self.statusLabel.alpha = 1.0;
self.statusLabel.text = @"正在載入中...";
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
self.statusLabel.text = @"載入完成...";
[UIView animateWithDuration:3.0 animations:^{
self.statusLabel.alpha = 0.0;
} completion:^(BOOL finished) {
NSLog(@"載入完成...");
}];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
self.statusLabel.alpha = 1.0;
self.statusLabel.text = @"載入失敗...";
}
以上code的作用是載入逍遙文工作室首頁,在開始載入時會出現「正在載入中…」字樣,完成載入時會出現「載入完成…」,經過3秒後會淡出消失,若失敗載入時則會出現「載入失敗…」。


隨意留個言吧:)~