亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS開發(fā)中WebView的基本使用方法簡介

 更新時(shí)間:2015年11月07日 10:02:39   作者:容芳志  
這篇文章主要介紹了iOS開發(fā)中WebView的基本使用方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下

1、使用UIWebView加載網(wǎng)頁
運(yùn)行XCode 4.3,新建一個(gè)Single View Application,命名為WebViewDemo。

2015117100010676.png (728×491)

2、加載WebView
在ViewController.h添加WebView成員變量和在ViewController.m添加實(shí)現(xiàn)

復(fù)制代碼 代碼如下:

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


運(yùn)行,這樣百度網(wǎng)頁就打開了

2015117100050458.png (368×716)

手機(jī)的網(wǎng)絡(luò)環(huán)境是實(shí)時(shí)變化的,網(wǎng)絡(luò)慢的時(shí)候,怎么提示用戶網(wǎng)頁正在打開呢?在網(wǎng)頁打開出錯(cuò)的時(shí)候怎么提示用戶呢?這時(shí)候我們就需要知道網(wǎng)頁什么時(shí)候打開的,
什么時(shí)候加載完成,什么時(shí)候出錯(cuò)了。那么我們需要實(shí)現(xiàn)這個(gè)<UIWebViewDelegate>協(xié)議
3、實(shí)現(xiàn)協(xié)議,在ViewController.h修改如下:

復(fù)制代碼 代碼如下:

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

    UIWebView *webView; 

@end 

按住control+command+向上鍵,切換到ViewController.m文件,這是我們?cè)谖募写蛉? (void) webView,就能看到如下實(shí)現(xiàn)方法:

2015117100107199.png (574×104)

4、UIWebView主要有下面幾個(gè)委托方法:

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

我們可以將activityIndicatorView放置到前面兩個(gè)委托方法中。

復(fù)制代碼 代碼如下:

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

buttonPress方法很簡單,調(diào)用我們開始定義好的loadWebPageWithString方法就行了:
復(fù)制代碼 代碼如下:

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

當(dāng)請(qǐng)求頁面出現(xiàn)錯(cuò)誤的時(shí)候,我們給予提示:
復(fù)制代碼 代碼如下:

- (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加入等待
復(fù)制代碼 代碼如下:

<strong>- (void) webViewDidStartLoad:(UIWebView *)webView
{
    //創(chuàng)建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>


加載完成或失敗時(shí),去掉loading效果
復(fù)制代碼 代碼如下:

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


運(yùn)行效果:

2015117100130691.png (368×716)

相關(guān)文章

最新評(píng)論