Introduction to the basic usage of webview in IOS development _ios

Source: Internet
Author: User
Tags uikit

1. Use UIWebView to load Web pages
run Xcode 4.3 and create a new single View application named Webviewdemo.

2, loading WebView
Add WebView member variable in VIEWCONTROLLER.H and add implementation in VIEWCONTROLLER.M

Copy Code code as follows:

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


Run, so the Baidu page opens

Mobile phone network environment is real-time change, the network is slow, how to prompt the user page is open? How to prompt the user when the Web page is open to 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 the ViewController.h modified as follows:

Copy Code code as follows:

#import <UIKit/UIKit.h>

@interface viewcontroller:uiviewcontroller<uiwebviewdelegate>
{
UIWebView *webview;
}
@end

Hold down the control+command+ up ARROW and switch to the VIEWCONTROLLER.M file, which is when we enter-(void) WebView in the file and we see the following implementation:

4, UIWebView mainly has the following several methods of delegation:

1,-(void) Webviewdidstartload: (UIWebView *) WebView, the method is executed when the load is started.
2,-(void) Webviewdidfinishload: (UIWebView *) WebView, execution of the method when loading is complete.
3,-(void) WebView: (UIWebView *) WebView didfailloadwitherror: (nserror *) Error, execute this method when loading error.

We can place the Activityindicatorview in the previous two delegate methods.

Copy Code code as follows:

-(void) Webviewdidstartload: (UIWebView *) WebView
{
[Activityindicatorview startanimating];
}
-(void) Webviewdidfinishload: (UIWebView *) WebView
{
[Activityindicatorview stopanimating];
}

The ButtonPress method is simple enough to invoke the Loadwebpagewithstring method that we started to define well:
Copy Code code as follows:

-(Ibaction) ButtonPress: (ID) sender
{
[TextField Resignfirstresponder];
[Self loadWebPageWithString:textField.text];

}

When the request page error occurs, we give a hint:
Copy Code code as follows:

-(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. Load Wait Interface
In order to give the user a more intuitive interface effect, we add the waiting loading interface to try
Join in waiting in Webviewdidstartload
Copy Code code as follows:

<strong>-(void) Webviewdidstartload: (UIWebView *) WebView
{
Create Uiactivityindicatorview Background 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 fails
Copy Code code as follows:

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


Operation Effect:

Related Article

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.