在iOS應用中使用UIWebView建立簡單的網頁瀏覽器介面_IOS

來源:互聯網
上載者:User

UIWebView是iOS sdk中一個最常用的控制項。是內建的瀏覽器控制項,我們可以用它來瀏覽網頁、開啟文檔等等。這篇文章我將使用這個控制項,做一個簡易的瀏覽器。如下圖:

我們建立一個Window-based Application程式命名為:UIWebViewDemo

UIWebView的loadRequest可以用來載入一個url地址,它需要一個NSURLRequest參數。我們定義一個方法用來載入url。在UIWebViewDemoViewController中定義下面方法:

複製代碼 代碼如下:

- (void)loadWebPageWithString:(NSString*)urlString
{
    NSURL *url =[NSURL URLWithString:urlString];
    NSLog(urlString);
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

在介面上放置3個控制項,一個textfield、一個button、一個uiwebview,布局如下:

在代碼中定義相關的控制項:webView用於展示網頁、textField用於地址欄、activityIndicatorView用於載入的動畫、buttonPress用於按鈕的點擊事件。

複製代碼 代碼如下:

@interface UIWebViewDemoViewController :UIViewController<UIWebViewDelegate> {   
    IBOutlet UIWebView *webView;
    IBOutlet UITextField *textField;
    UIActivityIndicatorView *activityIndicatorView;
    
}
- (IBAction)buttonPress:(id) sender;
- (void)loadWebPageWithString:(NSString*)urlString;
@end

使用IB關聯他們。

設定UIWebView,初始化UIActivityIndicatorView:

複製代碼 代碼如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    webView.scalesPageToFit =YES;
    webView.delegate =self;
    activityIndicatorView = [[UIActivityIndicatorView alloc]
                             initWithFrame : CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)] ;
    [activityIndicatorView setCenter: self.view.center] ;
    [activityIndicatorView setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleWhite] ;
    [self.view addSubview : activityIndicatorView] ;
    [self buttonPress:nil];
    // Do any additional setup after loading the view from its nib.
}

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];
}

動態擷取UIWebView高度
監聽 webView的 contentSize,每當contentSize的值改變時就去更改webView 的frame。
複製代碼 代碼如下:

[activityWebView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];

然後在回調方法裡改變webView的frame
複製代碼 代碼如下:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"contentSize"]) {
        webViewHeight = [[activityWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
        CGRect newFrame       = activityWebView.frame;
        newFrame.size.height  = webViewHeight;
        activityWebView.frame = newFrame;
        [mainTableView setTableHeaderView:activityWebView];
    }
}

在頁面消失時記得 remove 監聽對象,否則會閃退
複製代碼 代碼如下:

-(void)viewWillDisappear:(BOOL)antimated{
    [super viewWillDisappear:antimated];
    [activityWebView.scrollView removeObserver:self
forKeyPath:@"contentSize" context:nil];
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.