The use of iOS learning WebView (mainly the full-screen translucent implementation below)

Source: Internet
Author: User
Tags uikit

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" >
    1. #import <UIKit/UIKit.h>
    2. @interface Viewcontroller:uiviewcontroller
    3. {
    4. UIWebView *webview;
    5. }
    6. @end
[CPP]View Plaincopy
    1. Viewcontroller.m
[CPP]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. -(void) Viewdidload
    2. {
    3. [Super Viewdidload];
    4. WebView = [[UIWebView alloc] Initwithframe:cgrectmake (0, 0, 320, 480)];
    5. Nsurlrequest *request =[nsurlrequest requestwithurl:[nsurl urlwithstring:@"http://www.baidu.com"];
    6. [Self.view Addsubview:webview];
    7. [WebView Loadrequest:request];
    8. }

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
    1. #import <UIKit/UIKit.h>
    2. @interface viewcontroller:uiviewcontroller<uiwebviewdelegate>
    3. {
    4. UIWebView *webview;
    5. }
    6. @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
  1. <span style="font-family:arial, Verdana, Sans-serif;color: #333333;" >-(void) Webviewdidstartload: (UIWebView *) WebView
  2. {
  3. NSLog (@"webviewdidstartload");
  4. }
  5. -(void) Webviewdidfinishload: (UIWebView *) WebView
  6. {
  7. NSLog (@"webviewdidfinishload");
  8. }
  9. -(void) WebView: (UIWebView *) WebView didfailloadwitherror: (nserror *) Error
  10. {
  11. NSLog (@"didfailloadwitherror:%@", error);
  12. }
  13. </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
  1. <strong>-(void) Webviewdidstartload: (UIWebView *) WebView
  2. {
  3. //Create Uiactivityindicatorview back-bottom Translucent view
  4. UIView *view = [[UIView alloc] Initwithframe:cgrectmake (0, 0, 320, 480)];
  5. [View settag:108];
  6. [View Setbackgroundcolor:[uicolor Blackcolor];
  7. [View setalpha:0.5];
  8. [Self.view Addsubview:view];
  9. Activityindicator = [[Uiactivityindicatorview alloc] Initwithframe:cgrectmake (0.0f, 0.0f, 32.0f, 32.0f)];
  10. [Activityindicator SetCenter:view.center];
  11. [Activityindicator Setactivityindicatorviewstyle:uiactivityindicatorviewstylewhite];
  12. [View Addsubview:activityindicator];
  13. [Activityindicator startanimating];
  14. </strong>

Remove the loading effect when loading is complete or failed

[CPP]View Plaincopy
  1. <strong>-(void) Webviewdidfinishload: (UIWebView *) WebView
  2. {
  3. [Activityindicator stopanimating];
  4. UIView *view = (uiview*) [Self.view viewwithtag:108];
  5. [View Removefromsuperview];
  6. NSLog (@"webviewdidfinishload");
  7. }
  8. -(void) WebView: (UIWebView *) WebView didfailloadwitherror: (nserror *) Error
  9. {
  10. [Activityindicator stopanimating];
  11. UIView *view = (uiview*) [Self.view viewwithtag:108];
  12. [View Removefromsuperview];
  13. </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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.