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.百度.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>
運轉後果:
【iOS開辟中WebView的根本應用辦法簡介】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!