iOS開發中WebView的基本使用方法簡介_IOS

來源:互聯網
上載者:User

1、使用UIWebView載入網頁
運行XCode 4.3,建立一個Single View Application,命名為WebViewDemo。

2、載入WebView
在ViewController.h添加WebView成員變數和在ViewController.m添加實現

複製代碼 代碼如下:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    UIWebView *webView;
}
@end
ViewController.m
- (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修改如下:

複製代碼 代碼如下:

#import <UIKit/UIKit.h> 
 
@interface ViewController : UIViewController<UIWebViewDelegate> 

    UIWebView *webView; 

@end 

按住control+command+向上鍵,切換到ViewController.m檔案,這是我們在檔案中打入- (void) webView,就能看到如下實現方法:

4、UIWebView主要有下面幾個委託方法:

1、- (void)webViewDidStartLoad:(UIWebView *)webView;開始載入的時候執行該方法。
2、- (void)webViewDidFinishLoad:(UIWebView *)webView;載入完成的時候執行該方法。
3、- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;載入出錯的時候執行該方法。

我們可以將activityIndicatorView放置到前面兩個委託方法中。

複製代碼 代碼如下:

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [activityIndicatorView startAnimating] ;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [activityIndicatorView stopAnimating];
}

buttonPress方法很簡單,調用我們開始定義好的loadWebPageWithString方法就行了:
複製代碼 代碼如下:

- (IBAction)buttonPress:(id) sender
{
    [textField resignFirstResponder];
    [self loadWebPageWithString:textField.text];
    
}

當請求頁面出現錯誤的時候,我們給予提示:
複製代碼 代碼如下:

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription]  delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alterview show];
    [alterview release];
}

5、載入等待介面
為了給使用者更直觀的介面效果,我們加上等待的loading介面試試
在webViewDidStartLoad加入等待
複製代碼 代碼如下:

<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];
   </strong>


載入完成或失敗時,去掉loading效果
複製代碼 代碼如下:

<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>


運行效果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.