1. Loading Web pages with UIWebView
Run Xcode 4.3 and create a new single View application, named Webviewdemo.
2. Load WebView
Add WebView member variables in VIEWCONTROLLER.H and add implementation in VIEWCONTROLLER.M
[CPP]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
- #import <UIKit/UIKit.h>
- @interface Viewcontroller:uiviewcontroller
- {
- UIWebView *webview;
- }
- @end
[CPP]View Plaincopy
- Viewcontroller.m
[CPP]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
- -(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];
- }
Run, so Baidu Web page opens
The network environment of mobile phone is changing in real time, when the network is slow, how to prompt the user webpage is opening? How do I prompt the user when the Web page opens in error? That's when we need to know when the Web page opens,
When the load is complete and when it goes wrong. Then we need to implement this <UIWebViewDelegate> agreement
3, the implementation of the Agreement, in ViewController.h modified as follows:
[CPP]View Plaincopy
- #import <UIKit/UIKit.h>
- @interface viewcontroller:uiviewcontroller<uiwebviewdelegate>
- {
- UIWebView *webview;
- }
- @end
Press and hold the control+command+ up key to switch to the VIEWCONTROLLER.M file, which is the way we enter-(void) WebView in the file, we can see the following implementation:
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
4, realize these three methods, join NSLog.
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.
The three methods are implemented as follows:
[CPP]View Plaincopy
- <span style="font-family:arial, Verdana, Sans-serif;color: #333333;" >-(void) Webviewdidstartload: (UIWebView *) WebView
- {
- NSLog (@"webviewdidstartload");
- }
- -(void) Webviewdidfinishload: (UIWebView *) WebView
- {
- NSLog (@"webviewdidfinishload");
- }
- -(void) WebView: (UIWebView *) WebView didfailloadwitherror: (nserror *) Error
- {
- NSLog (@"didfailloadwitherror:%@", error);
- }
- </span>
Run Print:
2012-06-23 15:20:29.728 webviewdemo[1001:f803] Webviewdidstartload
2012-06-23 15:20:29.991 webviewdemo[1001:f803] Webviewdidfinishload
Let's try the error situation, turn off the WiFi and run the print results:
2012-06-23 15:23:58.939 webviewdemo[1087:f803] Webviewdidstartload
2012-06-23 15:23:59.016 webviewdemo[1087:f803] Webviewdidfinishload
The request result is not changed, why did the network turn off successfully? Cache? I changed 163.com to try and this is the real result came out:
2012-06-23 15:24:41.131 webviewdemo[1134:f803] Webviewdidstartload
2012-06-23 15:24:41.149 webviewdemo[1134:f803] didfailloadwitherror:error domain=nsurlerrordomain Code=-1009 "the Internet connection appears to be offline. " userinfo=0x6b41660 {nserrorfailingurlstringkey=http://www.163.com/, nserrorfailingurlkey=http://www.163.com/, Nslocalizeddescription=the Internet connection appears to be offline., nsunderlyingerror=0x6eae690 "the Internet Connection appears to be offline. "}
The connection was wrong and the Didfailloadwitherror was called.
5. Loading Wait interface
In order to give the user more intuitive interface effect, we add the waiting loading interface to try
Join the wait in Webviewdidstartload
[CPP]View Plaincopy
- <strong>-(void) Webviewdidstartload: (UIWebView *) WebView
- {
- //Create Uiactivityindicatorview back-bottom Translucent 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>
Remove the loading effect when loading is complete or failed
[CPP]View Plaincopy
- <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>
Operating effect:
Example code: http://download.csdn.net/detail/totogo2010/4391866
Copyright statement: This article by http://blog.csdn.net/totogo2010/ 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 (mainly the full-screen translucent implementation below)