1. Initialize WebView
-(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];
}
2.
Implement the Protocol, modify the following in ViewController.h #import <UIKit/UIKit.h>
@interface viewcontroller:uiviewcontroller<uiwebviewdelegate>
{
UIWebView *webview;
}
@end
several important functions in the UIWebView
1.-(void) Webviewdidstartload: (UIWebView *) Called when the page starts to load WebView
2.-(void) Webviewdidfinishload: (UIWebView *) WebView when page loading is complete
3.-(void) WebView: (UIWebView *) WebView didfailloadwitherror: (nserror *) error when the page is loaded incorrectly
First, add the following in the WebView instantiation of the Viewdidload
[webView setdelegate:self]; set proxy. This way, the three methods above can get a callback.
3. Loading Wait page
-(void) Webviewdidstartload: (UIWebView *) WebView
{
//Create Uiactivityindicatorview back-end translucent view
UIView *view = [[UIView alloc] Initwithframe:cgrectmake (0, 0, +, 480)];
[View settag:108];
[view Setbackgroundcolor:[uicolor blackcolor];
[view setalpha:0.5]; & nbsp
[Self.view Addsubview:view];
Activityindicator = [[ Uiactivityindicatorview Alloc] Initwithframe:cgrectmake (0.0f, 0.0f, 32.0f, 32.0f)];
[Activityindicator setCenter:view.center];
[activityindicator Setactivityindicatorviewstyle:uiactivityindicatorviewstylewhite]; &NBSP
[view addsubview:activityindicator];
[activityindicator Startanimating];
}
< Span style= "font-family:arial; font-size:14px; Line-height:26px "> when loading is complete or failed, remove the loading effect
< Span style= "font-family:arial; font-size:14px; Line-height:26px ">-(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];
}
Copyright statement: This article by http://blog.csdn.net/yangchen9931 Original, welcome reprint share. Please respect the author's labor, reproduced when the statement and the author of the blog link, thank you
The use of iOS learning WebView